Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This can be solved using an eval-parse construct, although I strongly advice you <strong>not</strong> to use this construct. It often causes more problems than anything else. But I couldn't get a decent do.call way of doing it. </p> <pre><code>vector_of_functions = NULL for (i in 1:4) { vector_of_functions = c(vector_of_functions, eval(parse(text=paste("function(number) func(number=number, coefficient=",i,")")))) } </code></pre> <p>Reason is as Aaron explained: everything within the function definition is taken as is until the function evaluation. </p> <p>Small remark: this is especially a <strong>list</strong> of functions, and not a vector. It's impossible to have a vector of type "function". It's also absolutely useless, as you have to select the function using the index [[]] before you can use it. Then I'd just add the argument instead of defining a function for every possible value of one of the arguments.</p> <p>So what you want to achieve is unclear, but if you want to apply func with different coefficients, I wonder why you don't simply do:</p> <pre><code>&gt; x &lt;- c(10,20,30) &gt; sapply(1:4,function(y)func(number=x,coefficient=y)) [,1] [,2] [,3] [,4] [1,] 10 20 30 40 [2,] 20 40 60 80 [3,] 30 60 90 120 </code></pre> <hr> <p>A variation on the theme by Marek (avoiding the parsing):</p> <pre><code>vector_of_functions = NULL for (i in 1:4) { vector_of_functions = c(vector_of_functions, eval(substitute(function(number) func(number=number, coefficient=i),list(i=i)))) } </code></pre> <p>The 1L etc. you get, just indicate they're exact integers (that take less memory space).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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