从.bat运行R脚本(批处理文件)

时间:2022-06-01 16:44:39

I am trying to run a r script using batch file. Currently I am using start"" "shortcut of R" to open R. However, I wanna R automatically run a r script that I have saved on the computer.

我正在尝试使用批处理文件运行r脚本。目前我正在使用开始“”R“的快捷方式打开R.但是,我想R自动运行我已保存在计算机上的r脚本。

Possibly R will be closed after running the script and user do not see R running.

可能在运行脚本后R将关闭,用户看不到R正在运行。

Is that possible? Thanks a lot!

那可能吗?非常感谢!

1 个解决方案

#1


17  

Rscript is a non-interactive variant of the standard R command, just designed for this kind of use.

Rscript是标准R命令的非交互式变体,仅为此类用途而设计。

For example, under windows you can define a launcher.bat like this :

例如,在windows下你可以像这样定义一个launcher.bat:

PATH PATH_TO_R/R-version/bin;%path%
cd PATH_TO_R_SCRIPT
Rscript myscript.R arg1 arg2

In myscript.R you add the code to read the arguments:

在myscript.R中,您添加代码以读取参数:

args <- commandArgs(trailingOnly = TRUE)
arg1 <- as.character(args[1])  
arg2 <- as.numeric(args[2])  

#1


17  

Rscript is a non-interactive variant of the standard R command, just designed for this kind of use.

Rscript是标准R命令的非交互式变体,仅为此类用途而设计。

For example, under windows you can define a launcher.bat like this :

例如,在windows下你可以像这样定义一个launcher.bat:

PATH PATH_TO_R/R-version/bin;%path%
cd PATH_TO_R_SCRIPT
Rscript myscript.R arg1 arg2

In myscript.R you add the code to read the arguments:

在myscript.R中,您添加代码以读取参数:

args <- commandArgs(trailingOnly = TRUE)
arg1 <- as.character(args[1])  
arg2 <- as.numeric(args[2])