Last active
March 13, 2019 15:37
-
-
Save arnauldvm/bd7789f1f5603eab31e04a3c039926e0 to your computer and use it in GitHub Desktop.
R retrieve args (~ $*) and script dir location (~ $0)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (sys.nframe()>0) { | |
script.path = sys.frame(1)[[ | |
switch(as.character(sys.call(1)[[1]]), | |
source='ofile', # Started with source(...) | |
debugSource='fileName', # Started with debugSource(...) | |
NA | |
) | |
]] | |
if (!exists("getargs")) { | |
# getargs array should be set before invoking source() from RStudio | |
getargs = c() | |
cat("set default getargs\n") | |
} | |
} else { # Started by Rscript | |
script.path = gsub("--file=", "", grep("--file=", commandArgs(trailingOnly=F), value=TRUE)) | |
getargs = commandArgs(trailingOnly=T) | |
} | |
script.dir <- dirname(script.path) | |
# Usage: | |
sample.option = gsub("--sample.option=", "", grep("--sample.option=", getargs, value=TRUE)) | |
siblingScript.path = file.path(script.dir, "sibling.r") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment