简体   繁体   English

使用可选参数在批处理文件上运行Rscript.exe

[英]Run Rscript.exe on batch file with optional arguments

I run a Windows batchfile (.bat) 我运行Windows批处理文件(.bat)

path_to_Rscript.exe file.R parameter1 parameter2 parameter3

on a file called file.R 在名为file.R的文件file.R

In file.R , I read the arguments using file.R ,我使用读取参数

commandArgs(trailingOnly = TRUE)
ABC <- args[1]
DEF <- args[2]
GHI <- args[3]

In case there is no parameter2 , calling only 如果没有parameter2 ,则仅调用

path_to_Rscript.exe file.R parameter1 parameter3

how can I make sure that parameter3 is not assigned to DEF ? 如何确定parameter3没有分配给DEF

So far, I used %% as a placeholder, but I am not sure whether this is a common approach. 到目前为止,我%%用作占位符,但是我不确定这是否是常用方法。

Is there a general placeholder for empty parameters? 空参数是否有通用占位符?

There is no general "no arg" placeholder, it depends on the progam. 没有通用的“ no arg”占位符,它取决于程序。

The answer is that you can't make sure p3 is not assigned to DEF without some assumptions, ie, if you only have two arguments then they are always going to be ABC and GHI. 答案是,在没有某些假设的情况下,您无法确保未将p3分配给DEF,即,如果您只有两个参数,则它们始终将是ABC和GHI。 In that case, you check for the length of commandArgs and adjust accordingly - that is a very narrow solution. 在这种情况下,您需要检查commandArgs的长度并相应地进行调整-这是一个非常狭窄的解决方案。

If you want to use optional positional arguments (which is generally a bad idea), they'd have to be at the end and then you really only get one for the same reason your facing now. 如果要使用可选的位置参数(通常是个坏主意),则必须将其放在最后,然后由于面对您的相同原因,您实际上只能得到一个。

The best way out of this conundrum is to use docopt. 解决这个难题的最好方法是使用docopt。 If you can't use docopt, then your stuck implementing pieces of a command-line parser which is a generally solved problem. 如果您不能使用docopt,那么您将无法实现命令行解析器,这是一个通常可以解决的问题。

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

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