简体   繁体   English

如何允许用户使用R的多个输入?

[英]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?: ")

You may find useful the tkentry function in package tcltk (for more examples see here ). 您可能会发现tcltk包中的tkentry函数很有用(有关更多示例,请参见此处 )。 There is also a guiDlg function in package svDialogs 包svDialogs中还有一个guiDlg函数

library(svDialogs)
display(guiDlg("SciViews-R", "My first dialog box with SciViews-R"))

Check this page for more.. 查看此页面了解更多..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM