I've been digging into several places for 2 simple needs but couldn't find a final answer.
I'm running an R script in batch mode. Not sure whether my solution is the best one but I'm using R CMD BATCH as per http://stat.ethz.ch/R-manual/R-patched/library/utils/html/BATCH.html included in a bat file.
First I'd like to have the directory where the R script is saved set up as the working directory rather than where the bat file is saved.
Secondly I'd like to divert all the output from the R script (csv files and charts) to a specific directory other than the working directory. I cannot find any options for such basic requirement.
The final idea is to be able to run the bat file across different computers no matter where the R script is saved.
Thanks
The cd newDir command will change the current working directory.
The working directory in R is the folder where you are working. Hence, it's the place (the environment) where you have to store your files of your project in order to load them or where your R objects will be saved.
You don't give code so my answer will be just an advise or what I would do for such job.
Rscript.exe
it is the way to go for batch script. R CMD is a sort of legacy tool.You can launch you bat file where you want and within it you go to R script location using cd for example you bat file can be like:
cd R_SCRIPT_PATH
Rscript youscript.R arg1 arg2
You can use one of the script argument as an output directory for your result files. For example inside your script you do somehing like this:
args <- commandArgs(trailingOnly = TRUE)
resultpath <- as.character(args[1])
.....
write.table(res1, file=paste(resultpath,'res1.csv',sep='/')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With