Skip to content

Instantly share code, notes, and snippets.

@arnauldvm
Last active March 13, 2019 15:37
Show Gist options
  • Save arnauldvm/bd7789f1f5603eab31e04a3c039926e0 to your computer and use it in GitHub Desktop.
Save arnauldvm/bd7789f1f5603eab31e04a3c039926e0 to your computer and use it in GitHub Desktop.
R retrieve args (~ $*) and script dir location (~ $0)
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