Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make readline wait for input in R

Tags:

r

I'm trying to make my code ask me for a "TRUE" or "FALSE" value before proceeding.

It currently works fine if I run it one line at a time, however when I run all the code at once in RStudio it simply proceeds without waiting for user input and records a value of "" for my parameter.

raw <- readline("TRUE or FALSE -- this is a validation run: ")


if (raw == "F" | raw == "FALSE" | raw == "False"){
validation <- F
} else{
  validation <- T
}
rm(raw)

Ideally, I would like an answer that works regardless of how I run it -- RScript, source within RStudio, or running it (i.e. selecting the code and pressing run or ctrl-enter).

like image 477
Hack-R Avatar asked Nov 24 '14 19:11

Hack-R


3 Answers

If you want to do this in interactive mode then you already have answers but not for use with Rscript. For that instance you need to send messages to the console with cat:

If this test file is named 'prompt.r' and is in the directory where you are working in a system console session:

cat("a string please: ");
a <- readLines("stdin",n=1);
cat("You entered")
 str(a);
cat( "\n" )

Then you can run it from the command line as

$ Rscript prompt.r

If you want an all-purpose script then this would run your script under interactive conditions and my script for non-interactive ones:

if (interactive() ){raw <- 
             readline("TRUE or FALSE -- this is a validation run: ")

                if (raw == "F" | raw == "FALSE" | raw == "False"){
               validation <- F
                 } else{
                     validation <- T
                       }
           rm(raw)  } else{
#  non-interactive
cat("a string please: ");
a <- readLines("stdin",n=1);
cat("You entered")
 str(a);
cat( "\n" )}
like image 173
IRTFM Avatar answered Sep 24 '22 07:09

IRTFM


Are you running the code by highlighting the lines and clicking run? If so, that could be your problem because R is line-by-line entering your code in the terminal.

Instead, write your script (or comment out the parts you're not testing) and click the source button. Then R waits for user response instead of inputting the line after readline() into readline().

I had the same problem as you, which prompted me to look for an answer. But then I tried this different method of executing the code and it worked.

like image 26
Sohum Daftary Avatar answered Sep 22 '22 07:09

Sohum Daftary


The reason why readline is "not waiting for input" is, according to the manual (?readline):

In non-interactive use the result is as if the response was RETURN and the value is "".

You have stumbled on one of the "surprising" features of R.

<rant> Why readline() works in "interactive mode" only is a complete mystery for me, since it's a perfectly acceptable use case to ask for user input from within a script. Python, for instance, gives you the input(prompt) function which you may invoke whenever you need it. </rant>

A relatively convenient way of getting around this mess is to define a function ("Every programming problem can be solved by yet another level of indirection"...):

user.input <- function(prompt) {
  if (interactive()) {
    return(readline(prompt))
  } else {
    cat(prompt)
    return(readLines("stdin", n=1))
  }
}

A positive side-effect is that you can add all sorts of nice input validation in user.input(). But I doubt that that was the intention behind the bizarre behaviour of readline().

like image 6
Laryx Decidua Avatar answered Sep 24 '22 07:09

Laryx Decidua