title: Need help writing a function for summary_table(dplyr::group_by
body: <p>I am trying to create a function that will minimize the number of times I will have to calculate all of the stats individually (Min, Median, Max, Mean, SD, and NAs). I have included the first two pieces of this large list, and how the list is being used. </p>
  
  <pre><code>  list("Child Age" =
         list("Min" = ~ min(.data$ChildAge,na_rm = TRUE),
              "Median" = ~ median(.data$ChildAge,na_rm = TRUE),
              "Mean &amp;plusmn; SD" = ~ qwraps2::mean_sd(.data$ChildAge,na_rm = TRUE),
              "Max" = ~ max(.data$ChildAge,na_rm = TRUE),
              "NA (Not factored in analysis)" =  ~  percent(sum(is.na(.data$ChildAge)) /length(.data$ChildAge))),
        "Child Gender" =
         list("Girl" = ~ qwraps2::n_perc(.data$ChildGender == "Girl", na_rm = TRUE),
              "Boy" = ~ qwraps2::n_perc(.data$ChildGender == "Boy", na_rm = TRUE))
  ......
  by_clinic_demographic &lt;- summary_table(dplyr::group_by(df, Clinic), demographic_summary)
  by_clinic_demographic
  </code></pre>
  
  <p>I have tried to design a function that will work:</p>
  
  <pre><code>analysis_func &lt;- function(x=df$StudyID) {
    list1 &lt;- list("Min" =   min(x,na.rm = TRUE),
              "Median" =  median(x,na.rm = TRUE),
              "Mean &amp;plusmn; SD" =  qwraps2::mean_sd(x,na_rm = TRUE),
              "Max" =  max(x,na.rm = TRUE),
            "NA (Not factored in analysis)" =   percent(sum(is.na(x)) /length(x)))
    #str(list1)
    return(list1)
  }
  </code></pre>
  
  <p>When I then go to call this function in a new list:</p>
  
  <pre><code>assessment_summary &lt;-
    list("Mother Age" = analysis_func(.data$MotherAge),, 
  </code></pre>
  
  <p>I get the error: <strong>Error: <code>x</code> must be a formula</strong></p>
  
  <p>When I add ~ after the = sign, so for example:</p>
  
  <pre><code>"Min" = ~  min(x,na.rm = TRUE)
  </code></pre>
  
  <p>I then get the error: <strong>Error in FUN(X[[i]], ...) : only defined on a data frame with all numeric variables</strong></p>
  
  <p>Here is a simplified version to highlight the issue that I am having:</p>
  
  <pre><code>analysis_func &lt;- function(x=df$StudyID) {
    list1 &lt;- list("Min" = ~ min(x,na.rm = TRUE),
              "Median" = ~ median(x,na.rm = TRUE),
              "Mean &amp;plusmn; SD" = ~ qwraps2::mean_sd(x,na_rm = TRUE),
              "Max" = ~ max(x,na.rm = TRUE),
            "NA (Not factored in analysis)" =  ~ percent(sum(is.na(x)) /length(x)))
    return(list1)
  }
  test_summary &lt;-
    list("Scores" = analysis_func(.data$StudyID))
  # test_stack &lt;- summary_table(dplyr::group_by(dataframe, s), test_summary)
  # test_stack
  
  n = c(2, 3, 5, 4,10,12,rep(10,4)) 
  s = c(rep("aa",5),rep("bb",5)) 
  dataframe &lt;- data.frame (n,s)
  
  
  
  test_summary2 &lt;-
    list("Scores" =
         list("Min" = ~ min(.data$n,na_rm = TRUE),
              "Median" = ~ median(.data$n,na_rm = TRUE),
              "Mean &amp;plusmn; SD" = ~ qwraps2::mean_sd(.data$n,na_rm = TRUE),
              "Max" = ~ max(.data$n,na_rm = TRUE),
              "NA (Not factored in analysis)" =  ~  percent(sum(is.na(.data$n)) /length(.data$n)))
    )
  
  test_stack &lt;- summary_table(dplyr::group_by(dataframe, s), test_summary2)
  test_stack
  </code></pre>
  
  <p>Any help would be appreciated.</p>