Using interactive R script in batch mode

By: | Comments: 1 Comment

Posted in categories: Computer Tips, Work related

According to R manual, http://cran.r-project.org/doc/manuals/fullrefman.pdf, page 411,

Value
A character vector of length one. Both leading and trailing spaces and tabs are stripped from the
result.
In non-interactive use the result is as if the response was RETURN and the value is “”.

So, if you have an R script that used readline as the way of grabbing keyboard input, it won’t be able to grab the proper input from your batch file that calls this script.

Alternatively, R is using command “scan” to do this job. see R manual page 443.

This change can be adapted into original R sources that used readline by a function:

readlineB <- function(myString0) {
print(myString0)
a <- scan(what=character(0),nmax=1)
return(a)
}

which means to grab the next one line in undetermined length of characters as the readline result.
Then by globally replace function readline by the customer defined function readlineB, your old
ways of sourcing R script by using following up lines as interactive input is working again.

One Comment

Leave a Reply