[英]How to allow multiple inputs from user using R?
For example, if I need that the user specifies the number of rows and columns of a matrix: 例如,如果我需要用户指定矩阵的行数和列数:
PROMPT: Number of rows?: 提示:行数?:
USER INPUT: [a number] 用户输入:[数字]
I need that R 'waits' for the input. 我需要R'等待'输入。 Then save [a number] into a variable v1.
然后将[数字]保存到变量v1中。 Next,
下一个,
PROMPT: Number of columns?: 提示:列数?:
USER INPUT: [another number] 用户输入:[另一个号码]
Also save [another number] into a variable v2. 还将[另一个数字]保存到变量v2中。 At the end, I will have two variables (v1, v2) that will be used in the rest of the code.
最后,我将有两个变量(v1,v2)将在其余代码中使用。
"readline" only works for one input at a time. “readline”一次只适用于一个输入。 I can't run the two lines together
我无法将两条线路连在一起
v1 <- readline("Number of rows?: ")
v2 <- readline("Number of columns?: ")
Any ideas or suggestions? 任何想法或建议?
Thank you in advance 先感谢您
You can combine those statements into a clause: 您可以将这些语句组合成一个子句:
{ v1 <- readline("Number of rows?: "); v2 <- readline("Number of columns?: ") }
Or generally, make them into a function: 或者通常,将它们变成一个函数:
readlines <- function(...) {
lapply(list(...), readline)
}
readlines("Number of rows?: ", "Number of columns?: ")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.