title: using mget within a function in R
body: <p>I am trying to write a simple function in in <code>r</code> that will search the <code>.GlobalEnv</code> for objects with a specific pattern in their names, then take that list and bind the elements into a data frame.</p>
  
  <p>When run separately, this works as expected:</p>
  
  <pre><code># create sample data
  
  df1_pattern_to_find &lt;- data.frame(a = 1, b = 2)
  df2_pattern_to_find &lt;- data.frame(a = 3, b = 4)
  
  # use mget to generate a list of objects
  
  list_of_objects &lt;- mget(ls(pattern="_pattern_to_find"))
  
  # bind the elements together into a data frame
  
  do.call("rbind", list_of_objects)
  
                      a b
  df1_pattern_to_find 1 2
  df2_pattern_to_find 3 4
  </code></pre>
  
  <p>However, when I wrap the above in a function it returns <code>NULL</code>:</p>
  
  <pre><code>gather_function &lt;- function() {
    list_of_objects &lt;- mget(ls(pattern="_pattern_to_find"))
  
    df &lt;- do.call("rbind", list_of_objects)
  
    df
  }
  
  gather_function()
  
  NULL
  </code></pre>
  
  <p>I have tried explicitly setting <code>envir</code> within <code>mget</code> to <code>.GlobalEnv</code> and that doesn't seem to be the issue.</p>
  
  <p>I know I am missing something simple here.</p>