Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我想批量运行R代码并等待用户输入_R_Batch File_Input - Fatal编程技术网

我想批量运行R代码并等待用户输入

我想批量运行R代码并等待用户输入,r,batch-file,input,R,Batch File,Input,我想批量运行一个程序,我想让它等待程序中的用户输入 这就是我所拥有的: library('XLConnect') FolderPath<-"C:/Documents/Testing/" load(paste('C:/Documents/Testing/Program.ml',sep=''));Run() 然后用户输入: >2001, 2002, 2003, 2004 程序将把这个向量保存在变量中,让我们调用y。 然后,当我加载编译后的函数时,我将使用y. 问题是我在cmd批处理中

我想批量运行一个程序,我想让它等待程序中的用户输入

这就是我所拥有的:

library('XLConnect')
FolderPath<-"C:/Documents/Testing/"
load(paste('C:/Documents/Testing/Program.ml',sep=''));Run()
然后用户输入:

>2001, 2002, 2003, 2004
程序将把这个向量保存在变量中,让我们调用y。
然后,当我加载编译后的函数时,我将使用y.

问题是我在cmd批处理中运行了这个R代码

Yukio,尝试以下示例中的函数
readline
(文档),其中用户输入两年,以逗号分隔

> years = readline('What are the years for this simulation? ')
What are the years for this simulation? 2001, 2002
> years = as.numeric(unlist(strsplit(years, ",")))
> years
[1] 2001 2002
顺便说一句,你的英语很棒

据我所知(如果这是错误的,请有人纠正我),但在使用R CMD BATCH执行脚本时,您不能在命令行向用户发送消息(它将所有输入和输出写入.Rout文件)。但使用Rscript.exe执行脚本实际上可以将内容发送到stdout。但是,为了在使用Rscript.exe执行时从用户处获取输入,您需要使用
文件('stdin')
作为与输入函数的连接,有趣的是,如果您随后尝试使用
源代码
函数从R解释器运行脚本,则该连接将无法工作。下面的示例向您展示了如何在一系列以逗号分隔的年份中提示用户,而不管脚本是使用Rscript.exe运行还是在交互模式下使用
source
函数运行

con <- if (interactive()) stdin() else file('stdin')
message('what are the years')
years <- scan(file=con, sep=',', nlines=1, quiet=TRUE)
print(years)

con您可以轻松处理批处理文件中的用户输入,而不是R中的用户输入。
创建一个批处理文件,例如
launcher.bat
,如下所示

ECHO I am Rscript launcher
set /p years= What are the years for this simulation?
cd R_SCRIPT_PATH
Rscript youscript.R %years%

用户可以写他想要的任意年份,它将在可变的年份中进行。然后在脚本中解析此变量,将其设置为有效的年份列表。

我已经尝试过了,不幸的是,readline()在批处理运行时不起作用。我真的很难完成我程序的这一部分。谢谢你的夸奖!我觉得自己在这里很傻。我不太明白。我应该如何在我的R代码中调用这个.bat?:)我正在使用Rscript而不是R CMD批来启动脚本。也许看看能更好地理解。嗯。。。我明白了,但这可能是一个问题,因为整个程序都在使用CMD BATCH运行。
ECHO I am Rscript launcher
set /p years= What are the years for this simulation?
cd R_SCRIPT_PATH
Rscript youscript.R %years%