Note that there are some explanatory texts on larger screens.

plurals
  1. POCasting closure to character in R
    text
    copied!<p>I have data and a working summary function:</p> <pre><code>[data:] x = runif(100) [function 1:] sapply(list("mean", "median", "sd"), function(f) array(do.call(f, list(x)), dimnames=list(f))) mean median sd 0.5198142 0.5566567 0.3048372 </code></pre> <p>The following also runs, but without giving the <code>dimnames</code>:</p> <pre><code>[function 2:] sapply(list(mean, median, sd), function(f) array(do.call(f, list(x)))) [1] 0.5198142 0.5566567 0.3048372 </code></pre> <p>I have unsuccessfully tried various combinations of <code>quote()</code>, <code>enquote()</code>, <code>substitute()</code>, <code>deparse()</code> etc to give me the same results with function 2 (where the functions to be applied are not quoted) as I got from function 1. I would like to find something of the following form, if it exists, which yields the same results as function 1:</p> <pre><code>[function 3:] sapply(list(mean, median, sd), function(f) array(do.call(f, list(x)), dimnames=list(SOMEFUNCTION(f)))) </code></pre> <p>My understanding of the problem is I need to use function that will quote <code>mean</code>, <code>median</code> and <code>sd</code> but after <code>f</code> has been partially evaluated; all my attempts at this have failed. </p> <p>I started off writing the function like function 3; rewriting as function 1 was the only way I could get the desired output, however I can't help but feel that there is some way to achieve this without the quotation marks in function 1.</p> <p>As this area is a little confusing to me, could you please explain why your solution works, in addition to providing the code.</p> <p>Thanks.</p>
 

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