Barry Rowlingson wrote:
On Wed, Jul 1, 2009 at 8:41 PM, Michael<comtech....@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

______________________________________________
R-help@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.

Reply via email to