Quantcast

Wait for keystroke or timeout

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Wait for keystroke or timeout

Prof J C Nash (U30A)
I would like to have a function that would wait either until a specified timeout (in
seconds preferably) or until a key is pressed. I've found such a function quite useful in
other programming environments in setting up dialogs with users or displaying results,
rather like a timed slideshow that can be speeded up by hitting a key.

Searching R-seek has led to wait() in the package 'audio', but when I try, for example,

    joe<-wait(readline("hit a key to continue"), timeout=6)

I am forced to wait the full timeout.

Probably someone has done this before and I'm just not using the right search terms.
Suggestions welcome.

Thanks in advance.

JN

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Wait for keystroke or timeout

biostatmatt


On Thu, 2010-05-27 at 19:08 -0400, Prof. John C Nash wrote:

> I would like to have a function that would wait either until a specified timeout (in
> seconds preferably) or until a key is pressed. I've found such a function quite useful in
> other programming environments in setting up dialogs with users or displaying results,
> rather like a timed slideshow that can be speeded up by hitting a key.
>
> Searching R-seek has led to wait() in the package 'audio', but when I try, for example,
>
>     joe<-wait(readline("hit a key to continue"), timeout=6)
>
> I am forced to wait the full timeout.
>
> Probably someone has done this before and I'm just not using the right search terms.
> Suggestions welcome.
>
> Thanks in advance.
>
> JN
>
> ______________________________________________
> [hidden email] 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.

John,

What operating system do you use? I don't know of an existing solution
to this problem (although someone else might). However, I recently
participated in a discussion on the r-devel list (cc'd) here

<http://r.789695.n4.nabble.com/Serial-connections-td2017474.html>,

about implementing a POSIX (Linux/Mac OSX) TTY (terminal) connection for
R to interface with a serial port. However, I am certain that a tty
connection for R could provide the functionality you are looking for, on
POSIX systems.

P.S. Maybe Simon (from the above discussion) has the right idea, that a
tty connection should exploit the full POSIX terminal API, then input
issues like this would have easy solutions...

P.P.S The Win32 serial communications API
<http://msdn.microsoft.com/en-us/library/ms810467.aspx>
also provides a "wait or event" blocking mechanism.

-Matt

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Wait for keystroke or timeout

biostatmatt
In reply to this post by Prof J C Nash (U30A)
On Thu, 2010-05-27 at 19:08 -0400, Prof. John C Nash wrote:

> I would like to have a function that would wait either until a specified timeout (in
> seconds preferably) or until a key is pressed. I've found such a function quite useful in
> other programming environments in setting up dialogs with users or displaying results,
> rather like a timed slideshow that can be speeded up by hitting a key.
>
> Searching R-seek has led to wait() in the package 'audio', but when I try, for example,
>
>     joe<-wait(readline("hit a key to continue"), timeout=6)
>
> I am forced to wait the full timeout.
>
> Probably someone has done this before and I'm just not using the right search terms.
> Suggestions welcome.
>
> Thanks in advance.
>
> JN
>
> ______________________________________________
> [hidden email] 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.

John,

What operating system do you use? I don't know of a general solution
to this problem (although someone else might). If you are using Linux
you can use try this

> system("read -t 1 -n 1")

where -n indicates the number of characters to read and -t specifies the
timeout in seconds. Hence, this command will wait until either 1 second
has elapsed, or 1 character has been read from the keyboard.

Also, I recently participated in a discussion on the r-devel list about
implementing a POSIX (Linux/Mac OSX) TTY (terminal) connection for R to
interface with a serial port. I am certain that a tty connection for R
could provide the functionality you are looking for, on POSIX systems.
Unfortunately this feature is currently only available as a source code
patch for R (not as an R package).

P.S. Maybe Simon (from the r-devel discussion) has the right idea, that
a tty connection should exploit the full POSIX terminal API, then input
issues like this would have easy solutions...

-Matt

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Wait for keystroke or timeout

Prof J C Nash (U30A)
Matt's suggestion works in Linux (I use Ubuntu and Debian variants), but I haven't yet
been able to get it to work in Windows. In a DOS terminal, I can run Cygwin's blas.exe via

   blas -c "read -t 1 -n 1"

and get the right functioning, but when embedded in R in various ways, I get several error
messages that imply R is not finding or interpreting the command correctly.

As the details are arcane, please contact me off-line, and I'll report back to the list
when we have a solution.

However, if someone could try Matt's suggestion on a Mac and let me know outcome, that
would be helpful. Since .Platform allows me to determine OS type, I should be able to work
out a more or less platform-independent function.

JN

biostatmatt wrote:

> On Thu, 2010-05-27 at 19:08 -0400, Prof. John C Nash wrote:
>> I would like to have a function that would wait either until a specified timeout (in
>> seconds preferably) or until a key is pressed.
> .... If you are using Linux
> you can use try this
>
>> system("read -t 1 -n 1")
>
> where -n indicates the number of characters to read and -t specifies the
> timeout in seconds.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Wait for keystroke or timeout

Greg Snow-2
In reply to this post by Prof J C Nash (U30A)
This is really a user interface issue and the standard user interface is different between platforms.  Would tcltk (or RGTK or ...) be a possible solution for you?  tcltk is fairly consistent across platforms and does provide for this type of thing (you can have a button to press to continue and use the after function to send an automatic push if the user does not push it before a given time).

--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
[hidden email]
801.408.8111


> -----Original Message-----
> From: [hidden email] [mailto:r-help-bounces@r-
> project.org] On Behalf Of Prof. John C Nash
> Sent: Thursday, May 27, 2010 5:09 PM
> To: [hidden email]
> Subject: [R] Wait for keystroke or timeout
>
> I would like to have a function that would wait either until a
> specified timeout (in
> seconds preferably) or until a key is pressed. I've found such a
> function quite useful in
> other programming environments in setting up dialogs with users or
> displaying results,
> rather like a timed slideshow that can be speeded up by hitting a key.
>
> Searching R-seek has led to wait() in the package 'audio', but when I
> try, for example,
>
>     joe<-wait(readline("hit a key to continue"), timeout=6)
>
> I am forced to wait the full timeout.
>
> Probably someone has done this before and I'm just not using the right
> search terms.
> Suggestions welcome.
>
> Thanks in advance.
>
> JN
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Wait for keystroke or timeout

William Dunlap
In reply to this post by Prof J C Nash (U30A)
> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of Prof. John C Nash
> Sent: Friday, May 28, 2010 7:46 AM
> Cc: [hidden email]
> Subject: Re: [R] Wait for keystroke or timeout
>
> Matt's suggestion works in Linux (I use Ubuntu and Debian
> variants), but I haven't yet
> been able to get it to work in Windows.

Another sort of solution would be a timeout()
function.   timeout(expr, seconds=5), where expr is an
expression to be evaluated, would return the value of
expr if if could be evaluated in the given number of
seconds.  If the evaluation of expr were not done in
a given number of seconds timeout() would do something
else: throw an error or return something of class "timedOut"
or ???.

I wrote one for S+ a long time ago when our QA department
wanted a way to check that an infinite-loop bug was gone.
Here is an R verion of that code which works only on Unix,
as it relies on have a shell available and on being able
to use Unix signals.   It throws an error if there is a
timeout.  You can catch the error with try or tryCatch
or withCallingHandlers.

I don't know if there is a similar thing in R now.  There
is a setTimeLimit function but I haven't played with enough
to know if it can do this.

timeout
function(expr, seconds = 60)
{
        # Set up a background process that will send a signal
        # to the current R process after 'seconds' seconds.
        # Evaluate expr with an interrupt handler installed
        # to catch the interrupt.
        # If expr finishes before that time it will kill the killer.
        killer.pid <- system(intern = TRUE, paste(" (sleep", seconds,
                " ; kill -INT", Sys.getpid(),
                ")>/dev/null&\n echo $!"))
        on.exit(system(paste("kill", killer.pid, "> /dev/null 2>&1")))
        withCallingHandlers(expr, interrupt=function(...)stop("Timed
out", call.=FALSE))
}

Try it on Linux with
  > z <- try(silent=TRUE, timeout(readline(prompt="Hit me: "),
seconds=5))
  Hit me: 34
  > z
  [1] "34"
  > z <- try(silent=TRUE, timeout(readline(prompt="Hit me: "),
seconds=5))
  Hit me: > z
  [1] "Error : Timed out\n"
  attr(,"class")
  [1] "try-error"

I don't know if this sort of user-interface stuff belongs
in R itself, but killing jobs that go on for too long can
be useful.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com

> In a DOS terminal, I
> can run Cygwin's blas.exe via
>
>    blas -c "read -t 1 -n 1"
>
> and get the right functioning, but when embedded in R in
> various ways, I get several error
> messages that imply R is not finding or interpreting the
> command correctly.
>
> As the details are arcane, please contact me off-line, and
> I'll report back to the list
> when we have a solution.
>
> However, if someone could try Matt's suggestion on a Mac and
> let me know outcome, that
> would be helpful. Since .Platform allows me to determine OS
> type, I should be able to work
> out a more or less platform-independent function.
>
> JN
>
> biostatmatt wrote:
> > On Thu, 2010-05-27 at 19:08 -0400, Prof. John C Nash wrote:
> >> I would like to have a function that would wait either
> until a specified timeout (in
> >> seconds preferably) or until a key is pressed.
> > .... If you are using Linux
> > you can use try this
> >
> >> system("read -t 1 -n 1")
> >
> > where -n indicates the number of characters to read and -t
> specifies the
> > timeout in seconds.
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Loading...