Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Combine <code>attach</code> and <code>sys.source</code> to source into an environment and attach that environment. Here I have two functions in file <code>my_fun.R</code>:</p> <pre><code>foo &lt;- function(x) { mean(x) } bar &lt;- function(x) { sd(x) } </code></pre> <p>Before I load these functions, they are obviously not found:</p> <pre><code>&gt; foo(1:10) Error: could not find function "foo" &gt; bar(1:10) Error: could not find function "bar" </code></pre> <p>Create an environment and source the file into it:</p> <pre><code>&gt; myEnv &lt;- new.env() &gt; sys.source("my_fun.R", envir = myEnv) </code></pre> <p>Still not visible as we haven't attached anything</p> <pre><code>&gt; foo(1:10) Error: could not find function "foo" &gt; bar(1:10) Error: could not find function "bar" </code></pre> <p>and when we do so, they are visible, and because we have attached a <em>copy</em> of the environment to the search path the functions survive being <code>rm()</code>-ed:</p> <pre><code>&gt; attach(myEnv) &gt; foo(1:10) [1] 5.5 &gt; bar(1:10) [1] 3.027650 &gt; rm(list = ls()) &gt; foo(1:10) [1] 5.5 </code></pre> <p>I still think you would be better off with your own personal package, but the above might suffice in the meantime. Just remember the copy on the search path is just that, a <em>copy</em>. If the functions are fairly stable and you're not editing them then the above might be useful but it is probably more hassle than it is worth if you are developing the functions and modifying them.</p> <p>A second option is to just name them all <code>.foo</code> rather than <code>foo</code> as <code>ls()</code> will not return objects named like that unless argument <code>all = TRUE</code> is set:</p> <pre><code>&gt; .foo &lt;- function(x) mean(x) &gt; ls() character(0) &gt; ls(all = TRUE) [1] ".foo" ".Random.seed" </code></pre>
    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