简体   繁体   English

批处理R脚本 - 设置工作目录并选择输出文件夹

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

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. 我正在批处理模式下运行R脚本。 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. 不确定我的解决方案是否是最好的解决方案,但是我根据http://stat.ethz.ch/R-manual/R-patched/library/utils/html/BATCH.html使用R CMD BATCH文件。

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. 首先,我想将保存R脚本的目录设置为工作目录,而不是保存bat文件的位置。

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. 其次,我想将R脚本(csv文件和图表)的所有输出转移到工作目录以外的特定目录。 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. 最后的想法是能够在不同的计算机上运行bat文件,无论R脚本保存在何处。

Thanks 谢谢

You don't give code so my answer will be just an advise or what I would do for such job. 你没有提供代码,所以我的回答只是一个建议,或者我会为这份工作做些什么。

  1. Use Rscript.exe it is the way to go for batch script. 使用Rscript.exe这是批处理脚本的方法。 R CMD is a sort of legacy tool. R CMD是一种传统工具。
  2. You don't need to set or change the working directory. 您无需设置或更改工作目录。 It is a source of problems 这是一个问题的根源
  3. 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: 您可以在任意位置启动bat文件,并在其中使用cd转到R脚本位置,例如您的bat文件可以是:

     cd R_SCRIPT_PATH Rscript youscript.R arg1 arg2 
  4. 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='/') 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM