title: Using "summarise" (dplyr) with a function requiring a formula
body: <p>I am trying to generate a table of regression slopes generated by a custom function based on the mblm package (the function in the example here is a simplified version). The function requires a formula as argument and I would like to use dplyr summarise to apply it to grouped samples from a large data frame with many variables.
  The output should be a tibble of regression slopes for sample groups and response variables that I can pass to a heatmap function.</p>
  <pre><code>library (dplyr)
  
  # Example data
  
  test_data &lt;- 
      rbind (
          data.frame(ID=paste0(&quot;someName&quot;, c(1:9)), Sample_Type=&quot;type1&quot;, 
             A=seq(1,17, length.out=9),
             I=0.1^seq(1,1.8,length.out=9), 
             J=1-0.1^seq(1,1.8,length.out=9)),
          data.frame(ID=paste0(&quot;someName&quot;, c(10:15)), Sample_Type=&quot;type2&quot;, 
             A=seq(1,7, length.out=6), 
             I=0.1^(1-seq(1,1.5,length.out=6)),
             J=1-0.1^(1-seq(1,1.5,length.out=6))))
  
  # Define an independent and the responding variables - I would like to be able to easily test different independent variables
   
  idpVar &lt;- &quot;A&quot;
  respVar &lt;- test_data %&gt;% .[!names(.) %in% c(&quot;ID&quot;, &quot;Sample_Type&quot;, idpVar)] %&gt;% names()
  
  # Custom function generating numeric value of median slopes (simplified from mblm)
  
  medianSlope &lt;-
  function (formula, dataframe) 
  {
      if (missing(dataframe)) 
          dataframe &lt;- environment(formula)
      term &lt;- as.character(attr(terms(formula), &quot;variables&quot;)[-1])
      x = dataframe[[term[2]]]
      y = dataframe[[term[1]]]
      if (length(term) &gt; 2) {
          stop(&quot;Only linear models are accepted&quot;)
      }
      xx = sort(x)
      yy = y[order(x)]
      n = length(xx)
      slopes = c()
      smedians = c()
          for (i in 1:n) {
              slopes = c()
              for (j in 1:n) {
                  if (xx[j] != xx[i]) {
                    slopes = c(slopes, (yy[j] - yy[i])/(xx[j] - 
                      xx[i]))
                   }
              }
              smedians = c(smedians, median(slopes))
          }
          slope = median(smedians)
      
      slope
  }
  
  </code></pre>
  <pre><code># Custom function works with test dataframe and a single named dependent variable but &quot;group_by&quot; seems to be ignored:
  
  test_data %&gt;% group_by (Sample_Type) %&gt;% medianSlope( formula(paste(&quot;J&quot;, &quot;~&quot;, idpVar))  ,.)
  </code></pre>
  <p>Leaving the grouping issue aside for the moment, I tried to make  &quot;summarise&quot; work by generating a list of multiple formulas:</p>
  <blockquote>
  <p>paste(respVar, &quot;~&quot;, idpVar)
  [1] &quot;B ~ A&quot; &quot;C ~ A&quot; &quot;D ~ A&quot; &quot;E ~ A&quot; &quot;F ~ A&quot; &quot;G ~ A&quot; &quot;H ~ A&quot; &quot;I ~ A&quot; &quot;J ~ A&quot; &quot;K ~ A&quot; &quot;L ~ A&quot;</p>
  </blockquote>
  <p>However</p>
  <blockquote>
  <p>test_data %&gt;% summarise_at (respVar,  medianSlope(paste(respVar, &quot;~&quot;, idpVar), .))</p>
  </blockquote>
  <blockquote>
  <p>Error: $ operator is invalid for atomic vectors</p>
  </blockquote>
  <blockquote>
  <p>test_data %&gt;% summarise_at (respVar,  medianSlope(paste(get(respVar), &quot;~&quot;, get(idpVar)), .))</p>
  </blockquote>
  <blockquote>
  <p>Error in get(idpVar) : object 'A' not found</p>
  </blockquote>
  <p>I am relatively new to R and a bit lost. Can you help?</p>