Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Edit of 2013-08-05</strong></p> <p>Using <code>sapply()</code> instead of <code>lapply()</code>, simplifies this considerably:</p> <pre><code>foo4 &lt;- function(a=1, b=5, h='coconut') { frm &lt;- formals(sys.function()) sapply(names(frm), get, envir=sys.frame(sys.parent(0)), simplify=FALSE) } foo4(b=0, h='mango') </code></pre> <p>This, though, without <code>sapply()</code> <strong>or</strong> <code>lapply()</code> might be the more elegant solution:</p> <pre><code>foo5 &lt;- function(a=1, b=5, h='coconut') { modifyList(formals(sys.function()), as.list(match.call())[-1]) } foo5(b=0, h='mango') </code></pre> <hr> <p><strong>Original post (2011-11-04)</strong></p> <p>After casting about a bit, this looks to be the best solution. </p> <pre><code>foo &lt;- function(a=1, b=5, h='coconut') { frm &lt;- formals(foo) parms &lt;- lapply(names(frm), get, envir=sys.frame(sys.parent(0))) names(parms) &lt;- names(frm) return(parms) } foo(b=0, h='mango') # $a # [1] 1 # $b # [1] 0 # $h # [1] "mango" </code></pre> <p>There's some subtle stuff going on here with the way that <code>lapply</code> scopes/evaluates the calls that it constructs. The details are hidden in a call to <code>.Internal(lapply(X, FUN))</code>, but for a taste, compare these two calls:</p> <pre><code># With function matched by match.fun, search in sys.parent(0) foo2 &lt;- function(a=1, h='coconut') { lapply(names(formals()), get, envir = sys.parent(0)) } # With anonymous function, search in sys.parent(2) foo3 &lt;- function(a=1, h='coconut') { lapply(names(formals()), FUN = function(X) get(X, envir = sys.parent(2))) } foo4(a=0, h='mango') foo5(a=0, h='mango') </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload