[R] Getting user input without hitting enter

Greg Snow Greg.Snow at imail.org
Wed Nov 24 20:45:52 CET 2010

This is a user interface issue.  The standard command line user interfaces all wait for the user to hit enter before sending the information to the parser, so this cannot be done using the standard command line interface.

You could rewrite the source code, but that is probably overkill.  The better option is to use a different interface, there are several options, some of which do not require much work to set up (there still is some).  The windows GUI can have the graphics device catch individual keystrokes and the Tk interfaces (tcltk package) can do this also (plus probably others).  The playSudoku function in the sudoku package has examples of both of the above, look at the code (the important parts are near the end of the function) and you should be able to see ways to adapt it to your needs.

Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Kohleth Chia
> Sent: Wednesday, November 24, 2010 5:53 AM
> To: r-help at r-project.org
> Subject: [R] Getting user input without hitting enter
> Hi,
> I am writing a function which requires user to enter a single
> character. Something like:
> hello<-function(){
> 	input<-scan(n=1,what="a")
> 	if(input=="h")cat("Hello") # What goes after the 'if' is
> completely arbitrary, for simplicity sake I simply put a cat command
> here.
> }
> I want R to automatically finish scanning after the user has entered
> the 1st character, without hitting 'enter'.
> How do I do this?
> Thanks
> ------------
> KC
> ______________________________________________
> 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.

More information about the R-help mailing list