Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use the <code>commandArgs</code> function to get all the options that were passed by Rscript to the actual R interpreter and search them for <code>--file=</code>. If your script was launched from the path or if it was launched with a full path, the <code>script.name</code> below will start with a <code>'/'</code>. Otherwise, it must be relative to the <code>cwd</code> and you can concat the two paths to get the full path.</p> <p><b>Edit:</b> it sounds like you'd only need the <code>script.name</code> above and to strip off the final component of the path. I've removed the unneeded <code>cwd()</code> sample and cleaned up the main script and posted my <code>other.R</code>. Just save off this script and the <code>other.R</code> script into the same directory, <code>chmod +x</code> them, and run the main script.</p> <p><b>main.R</b>:</p> <pre><code>#!/usr/bin/env Rscript initial.options &lt;- commandArgs(trailingOnly = FALSE) file.arg.name &lt;- "--file=" script.name &lt;- sub(file.arg.name, "", initial.options[grep(file.arg.name, initial.options)]) script.basename &lt;- dirname(script.name) other.name &lt;- file.path(script.basename, "other.R") print(paste("Sourcing",other.name,"from",script.name)) source(other.name) </code></pre> <p><b>other.R</b>:</p> <pre><code>print("hello") </code></pre> <p><b>output</b>:</p> <pre><code>burner@firefighter:~$ main.R [1] "Sourcing /home/burner/bin/other.R from /home/burner/bin/main.R" [1] "hello" burner@firefighter:~$ bin/main.R [1] "Sourcing bin/other.R from bin/main.R" [1] "hello" burner@firefighter:~$ cd bin burner@firefighter:~/bin$ main.R [1] "Sourcing ./other.R from ./main.R" [1] "hello" </code></pre> <p>This is what I believe dehmann is looking for.</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. 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