library(ggmosaic)
library(dplyr)
library(purrr)
library(tidyr)
library(broom)
library(tibble)使用下面的代码,我想让函数输出整洁的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)}...as和这个一样...
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)
}
} 我已经尝试了其他一些变体,所以如果有任何帮助,我将不胜感激。
发布于 2017-02-26 15:45:26
当您想要返回多个项目时,请使用列表:
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) ) }https://stackoverflow.com/questions/42465686
复制相似问题