Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to allow multiple inputs from user using R?

Tags:

r

user-input

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

like image 836
Juan Avatar asked Mar 29 '10 18:03

Juan


2 Answers

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?: ")
like image 102
Shane Avatar answered Nov 11 '22 23:11

Shane


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..

like image 24
George Dontas Avatar answered Nov 12 '22 01:11

George Dontas