Mein aktueller Einstieg in ein R-Script. Es wird unterschieden ob das Script als Entwicklungsscript aus RStudio gestartet wird oder von der Kommandozeile. Je nachdem werden Kommandozeilenparameter unterschiedlich gesetzt oder interpretiert.
#!/usr/local/bin/Rscript args <- commandArgs(F) # Checks whether the script was executed from within RStudio or not. # In case of RStudio getwd() returns the working directory of the script. script.path <- ifelse(args[1] == "RStudio", getwd(), normalizePath(dirname(sub("^--file=", "", args[grep("^--file=", args)])))) if(!exists("init___", mode="name")) {source(file.path(script.path, "init.R"))} f.init() f.configuration <- function() { if (args[1] != "RStudio") { spec = matrix(c('csv', 'c', 1, "character", "Source CSV data file.", 'target', 't', 1, "character", "Target directory to store the analyze."), byrow=TRUE, ncol=5) return(getopt(spec)) } return(list(csv = "~/rawdata.csv", target = "~/reports")) } opt <- f.configuration()
Geschrieben von sascha am 7. Oktober 2017 18:32:01 CEST