Creating a Prompt/Answer system to input data into R

R

R Problem Overview


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!

R Solutions


Solution 1 - R

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))
  
}

Solution 2 - R

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:

Solution 3 - R

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.

Solution 4 - R

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.

Solution 5 - R

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!")
  }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVinterwooView Question on Stackoverflow
Solution 1 - RChaseView Answer on Stackoverflow
Solution 2 - Rpatr1ckmView Answer on Stackoverflow
Solution 3 - RshadowtalkerView Answer on Stackoverflow
Solution 4 - RdnlbrkyView Answer on Stackoverflow
Solution 5 - RBrandon RoseView Answer on Stackoverflow