How to allow multiple inputs from user using R?

◇◆丶佛笑我妖孽 提交于 2019-12-19 03:24:38

问题


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. Then save [a number] into a variable v1. Next,

PROMPT: Number of columns?:

USER INPUT: [another number]

Also save [another number] into a variable v2. At the end, I will have two variables (v1, v2) that will be used in the rest of the code.

"readline" only works for one input at a time. 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


回答1:


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



回答2:


You may find useful the tkentry function in package tcltk (for more examples see here). There is also a guiDlg function in package svDialogs

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

Check this page for more..



来源:https://stackoverflow.com/questions/2540232/how-to-allow-multiple-inputs-from-user-using-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!