Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I <em>think</em> this is due to you not defining any arguments to <code>f</code>, and thence how the <code>x</code> required for <code>print(x)</code> is looked for.</p> <p>In normal use <code>f()</code> will look for <code>x</code> in the global environment if it isn't supplied (and it isn't nor can it be as <code>f</code> takes no arguments).</p> <p>Inside <code>with()</code>, what happens is that any arguments needed for <code>f</code> will be taken from the <code>data</code> argument. But because your <code>f</code> doesn't take any arguments, the <code>x</code> in the list is never used. Instead, R reverts to usual behaviour and looks up <code>x</code> in the environment of <code>f</code>, the global environment, and of course it isn't there. The difference with <code>attach()</code> is that it explicitly adds an object containing an <code>x</code> to the search path that is in the global environment.</p> <p>If you write your function properly, following the mantra that you pass in any and all arguments you use within the function, then everything works as one would expect:</p> <pre><code>&gt; F &lt;- function(x) print(x) &gt; with(list(x = 42), F(x)) [1] 42 &gt; ls() [1] "f" "F" </code></pre> <p>If you already have the list or arguments need for the call, perhaps consider <code>do.call()</code> to set up the call for you, instead of using with. For example:</p> <pre><code>&gt; do.call(F, list(x = 42)) [1] 42 </code></pre> <p>You still need your function to be properly defined with arguments, as your <code>f</code> doesn't work:</p> <pre><code>&gt; do.call(f, list(x = 42)) Error in function () : unused argument(s) (x = 42) </code></pre>
    singulars
    1. This table or related slice is empty.
    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. 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