On 10/21/03 14:47, 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") > >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))readline() -- Jonathan Baron, Professor of Psychology, University of Pennsylvania Home page: http://www.sas.upenn.edu/~baron R page: http://finzi.psych.upenn.edu/
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")par(ask = TRUE) will do what you want. -- Cheers, Kevin --------------------------------------------------------------- Ko-Kang Kevin Wang Master of Science (MSc) Student SLC Tutor and Lab Demonstrator Department of Statistics University of Auckland New Zealand Homepage: http://www.stat.auckland.ac.nz/~kwan022 Ph: 373-7599 x88475 (City) x88480 (Tamaki)
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
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")Maybe just readline("Press Enter to continue")? Why give the user the choice?> > 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))readline(), but the return value needs to be cast from a character vector of length one to what you need, because you can't depend on the user to only give admissible values. Although your example is just that, Sys.sleep() gets you there directly.> > TIA, > > Ernie Adorio > Math Department > University of the Philippines > Diliman, Quezon City > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >-- Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Breiviksveien 40, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 93 93 e-mail: Roger.Bivand at nhh.no
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") 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)) TIA, Ernie Adorio Math Department University of the Philippines Diliman, Quezon City