Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

Creating a Prompt/Answer system to input data into R

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...