Creating a Prompt/Answer system to input data into 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))

}

Leave a Comment