Issue
I am taking over a script made just to run other scripts. The code used to do so is the following :
cl <- makeCluster(detectCores() - 1)
registerDoParallel(cl, cores = detectCores() - 1)
dopar_output <- foreach(x=listExec, .errorhandling = "pass") %dopar% {
source(x)
}
listExec being a vector with various script path.
I have been trying to update this to pass on a variable with the name of the script to be used for log purpose during each script execution, like so :
dopar_output <- foreach(x=listExec, .errorhandling = "pass") %dopar% {
Script <- basename(gsub(".R","",x))
source(x)
}
The goal is to have a "Script" variable available in the environment while each script runs to make sure the name of the script and the name used in the logs are the same. However, with the code above, the Script variable is written in the list dopar_output, where I cannot use it (or at least I don't know how to).
I'm open to any suggestions, my first try was to declare the Script variable within each script with the command :
basename(sys.frame(1)$ofile)
However, that does not seems to be working properly in my environment, being : The script is run by a master script, itself run through a Cron job, on a Unix server.
Solution
You could use the system(...)
call to pass arguments through commandArgs(...)
# script1.R
val <- commandArgs(trailingOnly=TRUE)
writeLines(val, "out1.txt")
# script2.R
val <- commandArgs(trailingOnly=TRUE)
writeLines(val, "out2.txt")
Define the arg
listExec <- c("script1.R", "script2.R")
dopar_output <- foreach(x=listExec, .errorhandling = "pass") %dopar% {
arg <- basename(gsub(".R", "", x))
system(paste("Rscript", x, arg))
}
Output in out1
and out2
# out1.txt
# script1
# out2.txt
# script2
Answered By - CPak