Batch R Script - setting the working directory and selecting the output folder

I dug in several places for 2 simple needs, but could not find a definitive answer.

I am running an R script in batch mode. Not sure if my solution is the best, but I use R CMD BATCH according to http://stat.ethz.ch/R-manual/R-patched/library/utils/html/BATCH.html included in the bat file .

First, I would like to have a directory in which the R script is saved, configured as a working directory, and not where the bat file is saved.

Secondly, I would like to redirect all the output from the R script (csv files and diagrams) to a specific directory other than the working directory. I can not find any options for such a basic requirement.

The final idea is to be able to run the bat file on different computers no matter where the R script is stored.

thanks

+6
source share
1 answer

You do not give the code, so my answer would be just advice or what I would do for such a job.

  • Use Rscript.exe , this is the path to the batch script. R CMD is a kind of legacy tool.
  • You do not need to set or change the working directory. This is a source of problems.
  • You can run the bat file wherever you want, and inside it you get into the R script with cd, for example, you can use the bat file:

     cd R_SCRIPT_PATH Rscript youscript.R arg1 arg2 
  • You can use one of the script arguments as the output directory for your result files. For example, inside your script, you do the following:

     args <- commandArgs(trailingOnly = TRUE) resultpath <- as.character(args[1]) ..... write.table(res1, file=paste(resultpath,'res1.csv',sep='/') 
+6
source

Source: https://habr.com/ru/post/946075/


All Articles