library(ggmosaic)
library(dplyr)
library(purrr)
library(tidyr)
library(broom)
library(tibble)
使用下面的代码,我希望该功能同时输出整齐的tibble和ggplot。我不确定如何在功能中使用"返回"以返回多件事。
我尝试了这样的事情...
Chifun<-function(var){
df<-happy%>%select(-id,-year,-age,-wtssall)%>%
map(~chisq.test(.x,happy[,var]))%>%
tibble(names=names(.),data=.)%>%
mutate(stats=map(data,tidy))%>%unnest(stats)
GG<-ggplot(df)+ geom_col(aes_string(x="names",y="p.value"))
return(df,GG)}
...以及这个...
Chifun<-function(var){
df<-happy%>%select(-id,-year,-age,-wtssall)%>% map(~chisq.test(.x,happy[,var]))
%>%tibble(names=names(.),data=.)%>%
mutate(stats=map(data,tidy))%>%unnest(stats)
return(df)
GG<-function(var){ggplot(df)+
geom_col(aes_string(x="names",y="p.value"))
return(GG)
}
}
我已经尝试了其他一些变体,因此任何帮助都将不胜感激。
当您要返回多个项目时,请使用列表:
Chifun<-function(var){
df<-happy %>% select(-id,-year,-age,-wtssall) %>%
map(~chisq.test(.x,happy[,var])) %>%
tibble(names=names(.),data=.) %>%
mutate(stats=map(data,tidy))%>%unnest(stats)
GG<-ggplot(df)+ geom_col(aes_string(x="names",y="p.value"))
return( list(dfrm = df,plotGG = GG) ) }