我多年前制作了一个脚本来跟踪我的财务状况,但它停止了工作。该错误似乎在readLines中。以前,它会接受输入,现在它停留在循环中,而不返回主程序。最小的工作示例如下:
#! /usr/bin/Rscript
userinput <- function(question) {
cat(question, "\n> ")
n <- readLines(con = "stdin", 1)
return(as.numeric(n))
}
userinput("Enter your age: ") -> age
print(age)
将此脚本另存为userinput.R
,然后chmod +x userinput.R
,然后./userinput.R
。这个例子用于打印年龄,但它等待更多输入。
我的系统:
> sessionInfo()
R version 3.5.0 (2018-04-23)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Manjaro Linux
Matrix products: default
BLAS: /usr/lib/libblas.so.3.8.0
LAPACK: /usr/lib/liblapack.so.3.8.0
locale:
[1] LC_CTYPE=pt_BR.UTF-8 LC_NUMERIC=C LC_TIME=pt_BR.UTF-8
[4] LC_COLLATE=pt_BR.UTF-8 LC_MONETARY=pt_BR.UTF-8 LC_MESSAGES=pt_BR.UTF-8
[7] LC_PAPER=pt_BR.UTF-8 LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=pt_BR.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_3.5.0 tools_3.5.0 yaml_2.1.19