0
library(ggmosaic)
library(dplyr)
library(purrr)
library(tidyr)
library(broom)
library(tibble)

使用下面的代码,我希望该函数同时输出 tidy tibble 和 ggplot。我不确定如何在函数中使用“return”来返回不止一件事。

我尝试过这样的事情......

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)
}
}    

我已经尝试了一些其他的变化,所以任何帮助将不胜感激。

4

1 回答 1

1

当您要返回多个项目时,请使用列表:

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) ) }
于 2017-02-26T07:45:26.417 回答