如何在可执行的R文件/ Rexec文件中获取用户输入?

时间:2019-07-24 21:32:27

标签: r rscript

我创建可执行R文件(Rexec文件),以在计算R统计信息时生成用户友好的界面(如果您不熟悉Rexec文件,请参见本教程http://www.r-datacollection.com/blog/Making-R-files-executable/)。在仅生成输出(例如运行计算等)的文件上可以正常工作,但是在需要用户输入的程序上则不能。

假设我想创建一个简单的程序,让用户设置两个值,对其求和并显示结果:

# Create function:
SumTwoNumbers <- function(){
 # Take user input:
 x <- as.numeric(readline("Enter the first number: "))
 y <- as.numeric(readline("Enter the second number: "))
 # Calc. and display sum:
 z <- x + y
 message(paste("The sum is:", z))
}
# Apply function:
SumTwoNumbers()
Sys.sleep(10) # Freeze the screen in 10 sec (only for Rexec-script)

这在RStudio / R中工作正常,但是当我尝试在Rexec文件中运行此代码时,我只会得到:

> Enter the first number:
> Enter the second number:
> The sum is: NA

有人对此有解决方案吗?

0 个答案:

没有答案
相关问题