2024-05-08 16:33:27 发布
网友
R中的函数readline类似于python中的raw_input,两者都允许传递交互参数。在
readline
raw_input
然而,当我在终端中运行R脚本时,它不起作用。在
下面是一个示例txt.R:
txt.R
#!/usr/bin/env Rscript x = readline('Hello?') print(x)
在终端中运行./txt.R,它只打印出:
./txt.R
Hello? [1] "" 不等待我的输入。那怎么解决呢?在
Hello? [1] ""
我们可以在从终端运行的脚本中使用readLines。例如:
readLines
#!/usr/bin/env Rscript cat("What is your name? ") x <- readLines("stdin", 1) cat(sprintf("Hello, %s!\n", x))
不幸的是,readline()只能在交互模式下以预期的方式工作。在
这是文档中的注释?值部分的readline
In non-interactive use the result is as if the response was RETURN and the value is "".
您可以测试运行此代码的模式是否是交互式的。在
if(interactive()) { print("In interactive mode") } else { print("Not in interactive mode") }
我们可以在从终端运行的脚本中使用
readLines
。例如:不幸的是,readline()只能在交互模式下以预期的方式工作。在
这是文档中的注释?值部分的readline
您可以测试运行此代码的模式是否是交互式的。在
相关问题 更多 >
编程相关推荐