Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is only a problem if you write functions that do not take arguments or the functionality relies on the scoping of variables outside the current frame. you either i) pass in objects you need in the function as arguments to that function, or ii) create those objects inside the function that uses them.</p> <p>Your <code>f</code> is coded incorrectly. If you possibly alter <code>x</code>, then you should pass <code>x</code> in, possibly setting a default of <code>NA</code> or similar if that is what you want the other side of the random flip to be.</p> <pre><code>f &lt;- function(x = NA) { if (runif(1) &gt; .5) x &lt;- 10 x } </code></pre> <p>Here we see the function works as per your second function, but by properly assigning <code>x</code> as an argument with appropriate default. Note this works even if we have another <code>x</code> defined in the global workspace:</p> <pre><code>&gt; set.seed(3) &gt; replicate(10, f()) [1] NA 10 NA NA 10 10 NA NA 10 10 &gt; x &lt;- 4 &gt; set.seed(3) &gt; replicate(10, f()) [1] NA 10 NA NA 10 10 NA NA 10 10 </code></pre> <p>Another benefit of this is that you can pass in an <code>x</code> if you want to return some other value instead of <code>NA</code>. If you don't need that facility, then defining <code>x &lt;- NA</code> <em>in</em> the function is sufficient.</p> <p>The above is predicated on what you actually want to do with <code>f</code>, which isn't clear from your posting and comments. If all you want to do is randomly return <code>10</code> or <code>NA</code>, define <code>x &lt;- NA</code>.</p> <p>Of course, this function is very silly as it can't exploit vectorisation in R - it is very much a scalar operation, which we know is slow in R. A better function might be</p> <pre><code>f &lt;- function(n = 1, repl = 10) { out &lt;- rep(NA, n) out[runif(n) &gt; 0.5] &lt;- repl out } </code></pre> <p>or</p> <pre><code>f &lt;- function(x, repl = 10) { n &lt;- length(x) out &lt;- rep(NA, n) out[runif(n) &gt; 0.5] &lt;- repl out } </code></pre> <p>Ross's example function was, I surmise, intentionally simple and silly to highlight the scoping issue - it should not be taken as an example of writing good R code, nor would it have been intended as such. Be aware of the scoping feature and code accordingly, and you won't get bitten. You might even find you can exploit this feature...</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. 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.
    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