Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run a function every second

Tags:

r

I want to run a function that takes less than one second to execute. I want to run it in a loop every second. I do not want to wait one second between running the function like Sys.sleep would do.

while(TRUE){
  # my function that takes less than a second to run
  Sys.sleep(runif(1, min=0, max=.8))  

  # wait for the remaining time until the next execution...
  # something here
}

I could record a starttime <- Sys.time() and do a comparison every iteration through the loop, something like this...

starttime <- Sys.time()
while(TRUE){
  if(abs(as.numeric(Sys.time() - starttime) %% 1) < .001){
    # my function that takes less than a second to run
    Sys.sleep(runif(1, min=0, max=.8))  
    print(paste("it ran", Sys.time()))
  }
}

But my function never seems to be executed.

I know python has a package to do this sort of thing. Does R also have one that I don't know about? Thanks.

like image 949
cory Avatar asked Mar 18 '16 14:03

cory


People also ask

How do you call a function after every second?

The setInterval() method calls a function at specified intervals (in milliseconds). The setInterval() method continues calling the function until clearInterval() is called, or the window is closed. 1 second = 1000 milliseconds.

How do you make a function run every second in JavaScript?

Wait using setTimeout One of the easiest way to achieve a 1 second delay is by using the setTimeout function that lives on the window global object. console.

How do you call a function repeatedly every 5 seconds in JavaScript?

The setInterval() method in JavaScript can be used to perform periodic evaluation of expression or call a JavaScript function.

How do you run a function every 5 seconds in TypeScript?

Use the setInterval() method to call a function every N seconds in TypeScript, e.g. setInterval(myFunction, seconds * 1000) . The first parameter the method takes is the function that will be called on a timer, and the second parameter is the delay in milliseconds.


Video Answer


2 Answers

Another non-blocking alternative worth mentioning is provided by library(later), via using later() recursive:

print_time = function(interval = 10) {
  timestamp()
  later::later(print_time, interval)
}

print_time()

The example is taken from here.

like image 66
ismirsehregal Avatar answered Oct 06 '22 18:10

ismirsehregal


Here are some alternatives. These do not block. That is you can still use the console to run other code while they are running.

1) tcltk Try after in the tcltk package:

library(tcltk)

run <- function () { 
  .id <<- tcl("after", 1000, run) # after 1000 ms execute run() again
  cat(as.character(.id), "\n")    # replace with your code
}

run()

Running this on a fresh R session gives:

after#0 
after#1 
after#2 
after#3 
after#4 
after#5 
after#6 
after#7 
...etc...

To stop it tcl("after", "cancel", .id) .

2) tcltk2 Another possibility is tclTaskSchedule in the tcltk2 package:

library(tcltk2)

test <- function() cat("Hello\n")  # replace with your function
tclTaskSchedule(1000, test(), id = "test", redo = TRUE)

Stop it with:

tclTaskDelete("test")

or redo= can specify the number of times it should run.

like image 25
G. Grothendieck Avatar answered Oct 06 '22 18:10

G. Grothendieck