89

I've created some R code for use by people who know nothing of R (though I'm pretty green myself). I've been having people paste in the initial data to the R console (with mixed results), and I was hoping to set up a more user friendly way for people to enter in data.

Ideally someone could sit down at the console, type in a command, and be prompted with specific questions on how to enter the data.

For example, a person loads up r and sees a prompt:

What is x value?

The person types in:

2

Next prompt:

What is y value?

Person types in:

3

Next prompt:

 What are T values?

Person types in:

 4,3,2,1

Next prompt:

What are V values?

Person types in :

4,5,6,9

And with these 4 newly defined variables (X,Y,T,V) R's next step is to run the pre-written code

X+Y
V+T

And in the console the answers pop up

5
8 8 8 10

And everyone is happy

My apologies as this is not a reproducible code kind of question, but I'm not sure how to approach making R ask questions as opposed to me asking question about R!

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
Vinterwoo
  • 3,843
  • 6
  • 36
  • 55

5 Answers5

86

Since this is supposed to be used as interactive code only, readline() can work for you. I did not add any error checking, but you'd probably want to do a fair amount of that to ensure proper input. Here's the core concept though:

fun <- function(){
  x <- readline("What is the value of x?")  
  y <- readline("What is the value of y?")
  t <- readline("What are the T values?")
  v <- readline("What are the V values?")

  x <- as.numeric(unlist(strsplit(x, ",")))
  y <- as.numeric(unlist(strsplit(y, ",")))
  t <- as.numeric(unlist(strsplit(t, ",")))
  v <- as.numeric(unlist(strsplit(v, ",")))

  out1 <- x + y
  out2 <- t + v

  return(list(out1, out2))

}
Chase
  • 67,710
  • 18
  • 144
  • 161
  • 3
    The code works great! I was a little confused on how to start it, but after looking at the notes on readline() I just added one last line to the above "if(interactive()) fun()" and it starts the prompt automatically. Thanks! – Vinterwoo Jun 13 '12 at 08:08
  • 1
    This is nice. I've been trying to understand just exactly what is going on with the unlist and strsplit functions, however. What is R doing here? – Seanosapien Dec 09 '16 at 13:14
  • 1
    @Seanosapien The strsplit is necessary to strip the commas out of the input if the user types in '"4,3,2,1"' as in the original example. It's not necessary if the input isn't in that format. – Lauren Fitch May 09 '17 at 18:38
85

See also ?menu from utils for a simple text base menu interface and prompt, which is also used in devtools.

Here is an example:

> menu(c("Yes", "No"), title="Do you want this?")
Do you want this? 

1: Yes
2: No

Selection:
patr1ckm
  • 1,176
  • 9
  • 12
  • However, the function 'menu' only includes limited choice. The question's inputs are infinite integer numbers. – Jiaxiang Oct 27 '21 at 03:04
7

Since this question was brought back from the dead, it's probably writing an updated answer.

If a GUI is at all helpful in this case, the Shiny package is now well-integrated with RStudio, and it would be very easy to implement this as a Shiny application. The website http://shiny.rstudio.com has more info, including examples and documentation.

shadowtalker
  • 12,529
  • 3
  • 53
  • 96
6

It may be overkill for this particular case, but the swirl package is good for interactively introducing R to beginners.

swirl is a software package for the R programming language that turns the R console into an interactive learning environment. Users receive immediate feedback as they are guided through self-paced lessons in data science and R programming.

The instructions for generating content can be found here: http://swirlstats.com/instructors.html.

dnlbrky
  • 9,396
  • 2
  • 51
  • 64
3

Here is an example if you want to use menu it inside another function!

fun<-function(){
  input<-NULL
  x<-NULL
  input<-menu(c("lowercase", "UPPERCASE"),title="What type of letters do want to assign to x?") + 1
  if (input == 1){
    x<-NULL
    message('Nothing assigned to x')
  }
  if (input == 2){
    x<-letters
    message('x is lowercase!')
  }
  if (input == 3){
    x<-letters
    message("x is UPPERCASE!")
  }
}