title: Returning More Than One Object in a Function - a Tidy Chisq.Test Tibble and Ggplot
body: <pre><code>library(ggmosaic)
  library(dplyr)
  library(purrr)
  library(tidyr)
  library(broom)
  library(tibble)
  </code></pre>
  
  <p>Using the code below, I would like the function to output both the tidy tibble as well as the ggplot. I'm not sure how to use "return" within a function to return more than one thing. </p>
  
  <p>I tried something like this...   </p>
  
  <pre><code>Chifun&lt;-function(var){ 
  df&lt;-happy%&gt;%select(-id,-year,-age,-wtssall)%&gt;% 
  map(~chisq.test(.x,happy[,var]))%&gt;% 
  tibble(names=names(.),data=.)%&gt;%
  mutate(stats=map(data,tidy))%&gt;%unnest(stats)
  GG&lt;-ggplot(df)+ geom_col(aes_string(x="names",y="p.value"))
  return(df,GG)}
  </code></pre>
  
  <p>...as well as this...</p>
  
  <pre><code>Chifun&lt;-function(var){
  df&lt;-happy%&gt;%select(-id,-year,-age,-wtssall)%&gt;% map(~chisq.test(.x,happy[,var]))
  %&gt;%tibble(names=names(.),data=.)%&gt;%
  mutate(stats=map(data,tidy))%&gt;%unnest(stats)
  return(df)
  GG&lt;-function(var){ggplot(df)+
  geom_col(aes_string(x="names",y="p.value"))
  return(GG)
  }
  }    
  </code></pre>
  
  <p>I've tried a few other variations, so any help would be appreciated. </p>