等待用户从终端输入 R [英] waiting for user input in R from terminal
本文介绍了等待用户从终端输入 R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我从命令行以 Rscipt myscript.R 的形式运行我的脚本并从标准输入读取输入时,我能够等待用户在 R 中输入.
I was able to wait for user input in R when running my script as Rscipt myscript.R from the command line as follows and reading the input from stdin.
cat("Enter word : ")
word <- readLines(file("stdin"),1)
print(word);
但是,当我尝试使用以下代码从终端执行此操作时,它只会转到下一行而不接受用户输入.我该如何克服?
However, when I try to do it from the terminal using the below code, it just goes to the next line without taking user input. How do I overcome this?
word <- readline(prompt="Enter a word: ")
print(word);
推荐答案
用户"输入是 readline
之后的行.试试这个:
The "user" input is the line after readline
.
Try this:
word <- readline(prompt="Enter a word: ")
Hello world!
print(word)
更新
在控制台中等待输入:
Update
To wait for input in the console:
word <- readline(prompt="Enter a word: "); print(word)
或
{
word <- readline(prompt="Enter a word: ")
print(word)
}
这篇关于等待用户从终端输入 R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文