Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Suppose this was my analysis pipeline: I want to generate 10 numbers from the normal distribution with mean <code>MU</code> and standard deviation <code>SD</code> and then do something else with them:</p> <pre><code>MU &lt;- 1 # the mean SD &lt;- .5 # standard deviation NUMBER_TO_GENERATE &lt;- 10 x &lt;- rnorm(NUMBER_TO_GENERATE, mean=MU, sd=SD) # ... more analysis here. </code></pre> <p>At the moment I copy-paste these commands into the R terminal. There are a few ways to "automate" this.</p> <h3>1. Write a function</h3> <p>I encompass my list of commands to execute into one big function, and put my parameters as function parameters:</p> <pre><code>myFunction &lt;- function( MU, SD, NUMBER_TO_GENERATE ) { x &lt;- rnorm(NUMBER_TO_GENERATE, mean=MU, sd=SD) # ... rest of analysis } </code></pre> <p>Now within R I can just do <code>myFunction(1, .5, 10)</code>, reducing the number of commands I have to type to 1.</p> <h3>2. Write a script</h3> <p>I could write a script file <code>myScript.r</code>. This is like a bash script except it's a list of R commands.</p> <p>I can <em>either</em> put my original list of commands in there, Or I could put my function in there plus an additional statement at the bottom <code>myFunction(1,.5,10)</code>.</p> <p>Then from <em>within</em> R, I can do:</p> <pre><code>source('myScript.r') </code></pre> <p>and it will run all the R commands in the script.</p> <h3>3. From the shell</h3> <p>If you want to source this script from the shell, I'd suggest having a file <code>myScript.r</code> with the function inside it.</p> <p>Then check out <a href="http://stat.ethz.ch/R-manual/R-devel/library/utils/html/Rscript.html">Rscript</a> (you can just <code>?Rscript</code> from within R). This comes installed R by default, and you use it for executing R commands from a unix/windows command line.</p> <p>For example:</p> <pre><code>[mathematical.coffee@bar ~]$ Rscript -e '1+1' [1] 2 </code></pre> <p>In particular, you could combine methods 1) and 2) with <code>Rscript</code> to do something like:</p> <pre><code>[mathematical.coffee@bar ~]$ Rscript -e 'source("myScript.R"); myFunction( 1, .5, 10 )' </code></pre> <p>to run your function. </p> <p>Or you could of course just include the <code>myFunction(1, .5, 10)</code> in your <code>myScript.R</code>, in which case you can just do <code>Rscript myScript.R</code>.</p> <p>The advantage of the former is if you wanted to do shell scripting (I only mention this because you mentioned bash scripts in your question). In a <em>bash</em> script we could do something like:</p> <pre><code>#!/bin/bash MU=1; SD=.5; NUM=10; Rscript -e "source('myScript.r'); myFunction($MU,$SD,$NUM)" </code></pre> <p>However I'd argue for not mixing bash scripts with R scripts - as I mentioned before, I only mention this option because you mentioned bash/unix scripts in your question.</p>
    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. 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