[R] interactive prompts

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Oct 20 13:23:55 CEST 2003


On Tue, 21 Oct 2003, Ernie Adorio wrote:

> 
> Am using R on a Linux box and am currently writing an interactive R script.
> 
> 1. How do I ask a user to press any key to continue ? I used a system call to 
> read but this only works if the Enter key is pressed:
>   print("Press any key to continue")
>   system("read")

You seem over-fond of the system() command!  Try

cat("Press enter key to continue\n")
foo <- readLines(stdin(), 1)

In general R does not have a character-by-character interface with a 
keyboard.

> 2. How do I get a string input from the user? Would like to see an R function,
> say askget():
> 
>   delay  =  askget("Enter delay in seconds")
>   system(paste( "sleep ", delay))

cat("Enter delay in seconds:\n")
Delay <- scan("", numeric(1))
Sys.sleep(Delay)

will do it (there are other more elegant ways such as

testit <- function()
{
  cat("Enter delay in seconds: ")
  Delay <- as.numeric(readLines(stdin(), 1))
  if(is.na(Delay)) stop("non-numeric input")
  Sys.sleep(Delay)
}

)


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




More information about the R-help mailing list