[R] timer in R?

Philippe Grosjean phgrosjean at sciviews.org
Thu Jul 2 06:31:44 CEST 2009


Barry Rowlingson wrote:
> On Wed, Jul 1, 2009 at 8:41 PM, Michael<comtech.usa at gmail.com> wrote:
>> Hi all,
>>
>> How could I set a timer in R, so that at fixed interval, the R program
>> will invoke some other functions to run some tasks?
>>
> 
>  Use timer events in the tcltk package:
> 
>> z=function(){cat("Hello you!\n");tcl("after",1000,z)}
>> tcl("after",1000,z)
> 
>  now every 1000ms it will say "Hello you!". I'm not sure how to stop
> this without quitting R. There's probably some tcl function that
> clears it. Maybe. Or not. Just make sure you don't *always* reschedule
> another event on every event.
> 
>  Barry

Here it is:

library(tcltk)
z <- function () { cat("Hello you!\n"); .id <<- tcl("after", 1000, z)}
.id <<- tcl("after", 1000, z)
tcl("after", "info", .id)   # To get info about this scheduled task
tcl("after", "cancel", .id) # To cancel the currently scheduled task

Tcl is rather simple :-)... and everything is documented in 'man after'.
Best,

Philippe Grosjean




More information about the R-help mailing list