R命令行将文件名传递给参数中的脚本(Windows)文件名、脚本、命令、参数

2023-09-06 18:15:46 作者:兮

我很难将文件名传递给我的 R 脚本.该文件是一个 csv 文件,其中包含脚本多次运行的批处理参数.我试图在此处包含它,以便用户无需编辑 R 脚本即可指定该文件的位置.

I'm having a hard time passing a filename to my R script. The file is a csv file with the batch parameters for multiple runs of the script. I am trying to include it here so that the user does not need to edit the R script in order to specify the location of that file.

我的 Windows 命令行语法是:

My Windows command line syntax is:

R CMD BATCH --slave "--args fn=batch.csv" myscript.r output.txt

在我的 R 脚本中,我最接近检索它的方法是:

The closest I have gotten to retrieving this in my R script is by doing:

eval(parse(file=commandArgs()[8])))
batch_args = read.table(fn, sep=",")

我尝试过commandArgs(trailingOnly=TRUE)parse(text=commandArgs()[8])等,没有运气.我看到的大多数文档都不适用于传递文件名.谁能想到解决办法?

I have experimented with commandArgs(trailingOnly=TRUE) and parse(text=commandArgs()[8]), etc., with no luck. Most of the documentation that I have seen does not apply specifically to passing filenames. Can anyone think of a solution?

推荐答案

正如我在评论中所说,我会使用 Rscript 而不是 R CMD BATCH:p>

As I said in my comment, I would use Rscript instead of R CMD BATCH:

Rscript myscript.R batch.csv

myscript.R 包含的位置:

where myscript.R contains:

args <- commandArgs(TRUE)
batch_args <- read.table(args[1], sep=",")
# loop over multiple runs
 
精彩推荐
图片推荐