Batch 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.
我正在以批处理模式运行R脚本.不知道我的解决方案是否是最好的解决方案,但是我正在根据
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.
首先,我想将R脚本的保存目录设置为工作目录,而不是bat文件的保存目录.
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脚本(csv文件和图表)的所有输出转移到工作目录以外的特定目录.对于这种基本要求,我找不到任何选择.
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.
最终的想法是无论R脚本保存在哪里,都能在不同的计算机上运行bat文件.
The final idea is to be able to run the bat file across different computers no matter where the R script is saved.
谢谢
推荐答案
您不提供代码,因此我的答案将只是一个建议或我将要完成的工作.
You don't give code so my answer will be just an advise or what I would do for such job.
- 使用
Rscript.exe
是批处理脚本的一种方法.R CMD是一种传统工具. - 您不需要设置或更改工作目录.这是问题的根源
-
您可以在所需的位置启动bat文件,并在其中使用cd转到R脚本位置,例如bat文件可以像这样:
- Use
Rscript.exe
it is the way to go for batch script. R CMD is a sort of legacy tool. - You don't need to set or change the working directory. It is a source of problems
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='/')
这篇关于Batch R脚本-设置工作目录并选择输出文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!