Dear all, I've found this nice code fragment on the web (from Jan T. Kim if I'm correct): hitReturn <- function(msg) { invisible(readline(sprintf("%s -- hit return", msg))); } But it does not seem to work in non-interactive mode ( I mean, when I start a script like this: R --vanilla < script.R ) It simply continues, I guess the stdin is not connected? Does somebody know how to make it work. My script plots a few graphs (in X11 windows) and I don't want them to disappear immediately. A Sys.sleep(infinite) would probably work, but I thought there might be a better solution? Thanks, Jonne.
You have not told us your version of R. In earlier versions of R readline() reads a line from the script (and input buffering affects which line: a number of packages fail their example checks on some platforms because of this). In R-devel it returns "" immediately. To make this work you need to use Rscript, which distinguishes stdin() and "stdin". Try out % Rscript foo.R where foo.R is message("a") invisible(readLines("stdin", n=1)) message("b") and see ?stdin and ?file for why it works. On Fri, 5 Feb 2010, Jonne Zutt wrote:> Dear all, > > I've found this nice code fragment on the web > (from Jan T. Kim if I'm correct): > > hitReturn <- function(msg) > { > invisible(readline(sprintf("%s -- hit return", msg))); > } > > But it does not seem to work in non-interactive mode > ( I mean, when I start a script like this: R --vanilla < script.R ) > > It simply continues, I guess the stdin is not connected? > > Does somebody know how to make it work. > > My script plots a few graphs (in X11 windows) and I don't want them to > disappear immediately. > A Sys.sleep(infinite) would probably work, but I thought there might > be a better solution? > > Thanks, > Jonne.-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
On 05/02/2010 4:31 AM, Jonne Zutt wrote:> Dear all, > > I've found this nice code fragment on the web > (from Jan T. Kim if I'm correct): > > hitReturn <- function(msg) > { > invisible(readline(sprintf("%s -- hit return", msg))); > } > > But it does not seem to work in non-interactive mode > ( I mean, when I start a script like this: R --vanilla < script.R ) > > It simply continues, I guess the stdin is not connected?stdin is connected to script.R. That's what the input redirection does.> Does somebody know how to make it work.You need to find some other way to communicate with R besides stdin, either for your script or for your response to the request to wait. It's probably easiest to do the former. Just start R --vanilla, then type source("script.R"). Then stdin is still your terminal, and the hitReturn function will work. Duncan Murdoch> > My script plots a few graphs (in X11 windows) and I don't want them to > disappear immediately. > A Sys.sleep(infinite) would probably work, but I thought there might > be a better solution? > > Thanks, > Jonne. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.