title: need to call a function with the argument the group_by variable
body: <p>I am exploring data and concluded a function would be useful.  The attached code uses the iris dataset and there are 4 attempts at creating a usable function but each one gives the error that the group_by variable was not found.</p>
  
  <p>Here's the code.  The first segment is just plain code that works.  The next 4 are failed functions.</p>
  
  <pre><code>#  dplyr group by programically
  library(dplyr)
  data(iris)
  dtbl &lt;- tbl_df(iris)
  dtbl %&gt;% group_by(Species) %&gt;%
       mutate(SpeciesArea = Sepal.Length * Sepal.Width) %&gt;%
       summarise(MeanSepalLenght = mean(Sepal.Length), MeanSepalWidth =
                      mean(Sepal.Width))
  
  
  datafun &lt;- function(groupbyvar)
  library(dplyr)
  data(iris)
  groupbyvar &lt;- as.character(groupbyvar)
  dtbl &lt;- tbl_df(iris)
  dtbl %&gt;% group_by_(groupbyvar) %&gt;%
       mutate(SpeciesArea = Sepal.Length * Sepal.Width) %&gt;%
       summarise(MeanSepalLenght = mean(Sepal.Length), MeanSepalWidth =
                      mean(Sepal.Width))
  
  bb &lt;- datafun("Species")
  
  datafun &lt;- function(groupbyvar)
       library(dplyr)
  data(iris)
  groupbyvar &lt;- as.character(groupbyvar)
  dtbl &lt;- tbl_df(iris)
  dtbl %&gt;% group_by_("groupbyvar") %&gt;%
       mutate(SpeciesArea = Sepal.Length * Sepal.Width) %&gt;%
       summarise(MeanSepalLenght = mean(Sepal.Length), MeanSepalWidth =
                      mean(Sepal.Width))
  
  bb &lt;- datafun("Species")
  
  
  datafun &lt;- function(groupbyvar)
       library(dplyr)
  data(iris)
  groupbyvar &lt;- as.character(groupbyvar)
  dtbl &lt;- tbl_df(iris)
  dtbl %&gt;% group_by(groupbyvar) %&gt;%
       mutate(SpeciesArea = Sepal.Length * Sepal.Width) %&gt;%
       summarise(MeanSepalLenght = mean(Sepal.Length), MeanSepalWidth =
                      mean(Sepal.Width))
  
  bb &lt;- datafun("Species")
  
  datafun &lt;- function(groupbyvar)
       library(dplyr)
  data(iris)
  groupbyvar &lt;- as.character(groupbyvar)
  dtbl &lt;- tbl_df(iris)
  dtbl %&gt;% group_by("groupbyvar") %&gt;%
       mutate(SpeciesArea = Sepal.Length * Sepal.Width) %&gt;%
       summarise(MeanSepalLenght = mean(Sepal.Length), MeanSepalWidth =
                      mean(Sepal.Width))
  
  bb &lt;- datafun("Species")
  
  and the console output
  
  &gt; #  dplyr group by programically
  &gt; library(dplyr)
  &gt; data(iris)
  &gt; dtbl &lt;- tbl_df(iris)
  &gt; dtbl %&gt;% group_by(Species) %&gt;%
  +      mutate(SpeciesArea = Sepal.Length * Sepal.Width) %&gt;%
  +      summarise(MeanSepalLenght = mean(Sepal.Length), MeanSepalWidth =
  +                     mean(Sepal.Width))
  Source: local data frame [3 x 3]
  
       Species MeanSepalLenght MeanSepalWidth
        (fctr)           (dbl)          (dbl)
  1     setosa           5.006          3.428
  2 versicolor           5.936          2.770
  3  virginica           6.588          2.974
  &gt; 
  &gt; 
  &gt; datafun &lt;- function(groupbyvar)
  + library(dplyr)
  &gt; data(iris)
  &gt; groupbyvar &lt;- as.character(groupbyvar)
  Error: object 'groupbyvar' not found
  &gt; dtbl &lt;- tbl_df(iris)
  &gt; dtbl %&gt;% group_by_(groupbyvar) %&gt;%
  +      mutate(SpeciesArea = Sepal.Length * Sepal.Width) %&gt;%
  +      summarise(MeanSepalLenght = mean(Sepal.Length), MeanSepalWidth =
  +                     mean(Sepal.Width))
   Hide Traceback
  
   Rerun with Debug
   Error in as.lazy_dots(list(...)) : object 'groupbyvar' not found 
  12 as.lazy_dots(list(...)) 
  11 lazyeval::all_dots(.dots, ...) 
  10 group_by_prepare(.data, ..., .dots = .dots, add = add) 
  9 group_by_.data.frame(., groupbyvar) 
  8 group_by_(., groupbyvar) 
  7 function_list[[i]](value) 
  6 freduce(value, `_function_list`) 
  5 `_fseq`(`_lhs`) 
  4 eval(expr, envir, enclos) 
  3 eval(quote(`_fseq`(`_lhs`)), env, env) 
  2 withVisible(eval(quote(`_fseq`(`_lhs`)), env, env)) 
  1 dtbl %&gt;% group_by_(groupbyvar) %&gt;% mutate(SpeciesArea = Sepal.Length * 
      Sepal.Width) %&gt;% summarise(MeanSepalLenght = mean(Sepal.Length), 
      MeanSepalWidth = mean(Sepal.Width)) 
  &gt; 
  &gt; bb &lt;- datafun("Species")
  &gt; 
  &gt; datafun &lt;- function(groupbyvar)
  +      library(dplyr)
  &gt; data(iris)
  &gt; groupbyvar &lt;- as.character(groupbyvar)
  Error: object 'groupbyvar' not found
  &gt; dtbl &lt;- tbl_df(iris)
  &gt; dtbl %&gt;% group_by_("groupbyvar") %&gt;%
  +      mutate(SpeciesArea = Sepal.Length * Sepal.Width) %&gt;%
  +      summarise(MeanSepalLenght = mean(Sepal.Length), MeanSepalWidth =
  +                     mean(Sepal.Width))
   Hide Traceback
  
   Rerun with Debug
   Error: unknown column 'groupbyvar' 
  12 stop(structure(list(message = "unknown column 'groupbyvar'", 
      call = NULL, cppstack = NULL), .Names = c("message", "call", 
  "cppstack"), class = c("Rcpp::exception", "C++Error", "error", 
  "condition"))) 
  11 grouped_df_impl(data, unname(vars), drop) 
  10 grouped_df(groups$data, groups$groups) 
  9 group_by_.data.frame(., "groupbyvar") 
  8 group_by_(., "groupbyvar") 
  7 function_list[[i]](value) 
  6 freduce(value, `_function_list`) 
  5 `_fseq`(`_lhs`) 
  4 eval(expr, envir, enclos) 
  3 eval(quote(`_fseq`(`_lhs`)), env, env) 
  2 withVisible(eval(quote(`_fseq`(`_lhs`)), env, env)) 
  1 dtbl %&gt;% group_by_("groupbyvar") %&gt;% mutate(SpeciesArea = Sepal.Length * 
      Sepal.Width) %&gt;% summarise(MeanSepalLenght = mean(Sepal.Length), 
      MeanSepalWidth = mean(Sepal.Width)) 
  &gt; 
  &gt; bb &lt;- datafun("Species")
  &gt; 
  &gt; 
  &gt; datafun &lt;- function(groupbyvar)
  +      library(dplyr)
  &gt; data(iris)
  &gt; groupbyvar &lt;- as.character(groupbyvar)
  Error: object 'groupbyvar' not found
  &gt; dtbl &lt;- tbl_df(iris)
  &gt; dtbl %&gt;% group_by(groupbyvar) %&gt;%
  +      mutate(SpeciesArea = Sepal.Length * Sepal.Width) %&gt;%
  +      summarise(MeanSepalLenght = mean(Sepal.Length), MeanSepalWidth =
  +                     mean(Sepal.Width))
   Hide Traceback
  
   Rerun with Debug
   Error: unknown column 'groupbyvar' 
  13 stop(structure(list(message = "unknown column 'groupbyvar'", 
      call = NULL, cppstack = NULL), .Names = c("message", "call", 
  "cppstack"), class = c("Rcpp::exception", "C++Error", "error", 
  "condition"))) 
  12 grouped_df_impl(data, unname(vars), drop) 
  11 grouped_df(groups$data, groups$groups) 
  10 group_by_.data.frame(.data, .dots = lazyeval::lazy_dots(...), 
      add = add) 
  9 group_by_(.data, .dots = lazyeval::lazy_dots(...), add = add) 
  8 group_by(., groupbyvar) 
  7 function_list[[i]](value) 
  6 freduce(value, `_function_list`) 
  5 `_fseq`(`_lhs`) 
  4 eval(expr, envir, enclos) 
  3 eval(quote(`_fseq`(`_lhs`)), env, env) 
  2 withVisible(eval(quote(`_fseq`(`_lhs`)), env, env)) 
  1 dtbl %&gt;% group_by(groupbyvar) %&gt;% mutate(SpeciesArea = Sepal.Length * 
      Sepal.Width) %&gt;% summarise(MeanSepalLenght = mean(Sepal.Length), 
      MeanSepalWidth = mean(Sepal.Width)) 
  &gt; 
  &gt; bb &lt;- datafun("Species")
  &gt; 
  &gt; datafun &lt;- function(groupbyvar)
  +      library(dplyr)
  &gt; data(iris)
  &gt; groupbyvar &lt;- as.character(groupbyvar)
  Error: object 'groupbyvar' not found
  &gt; dtbl &lt;- tbl_df(iris)
  &gt; dtbl %&gt;% group_by("groupbyvar") %&gt;%
  +      mutate(SpeciesArea = Sepal.Length * Sepal.Width) %&gt;%
  +      summarise(MeanSepalLenght = mean(Sepal.Length), MeanSepalWidth =
  +                     mean(Sepal.Width))
  Source: local data frame [1 x 3]
  
    "groupbyvar" MeanSepalLenght MeanSepalWidth
           (chr)           (dbl)          (dbl)
  1   groupbyvar        5.843333       3.057333
  &gt; 
  &gt; bb &lt;- datafun("Species")
  &gt; 
  </code></pre>
  
  <p>Any help is appreciated.  I have spent most of the day in futile attempts to make this work.  Nothing discovered that solved the problem,  Of course I could just copy, paste, search and replace but that is a last resort.  I am hopeful that creating meaningful variables from the data will reveal the confounder I suspect is hiding in the data.</p>
  
  <p>Thanks
  Carl Sutton</p>