Description Usage Arguments Details Value Additional methods Examples
Run an R function in the background, possibly after a delay. The current
version uses the Tcl event loop. It was inspired by similar
functionality in the tcltk2
package.
1 2 3 4 5 6 |
ms |
Amount of time to wait before running the function, in milliseconds. An integer scalar. Use zero for immediate execution. |
fun |
Function to run. Note that the function is run in the global environment, so it is good practice not to use functions from packages directly. See the examples below. |
args |
Arguments to pass to the function, a list. As the function runs in the global environment, it does not have access to the objects in the calling environment. But you can pass arguments to it here. |
redo |
How many times to re-run the function. Zero means running
it only once, and |
task |
Task id. |
Note that this does not mean parallelism. The scheduled function runs in the main R process, after the specified time, whenever R is free of other work. Also, while the scheduled function runs, no other R computation can be done. If you use R interactively, then your command prompt will “freeze” while the scheduled function runs.
Thus, after
is best for running very short processes, at least
for interactive use.
A task id that you can use in after$info
and
after$cancel
. It is returned invisibly.
after$info(task)
will display some information about the task.
after$list()
lists all scheduled tasks.
after$cancel(task)
cancels a task.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | # simple example, runs after a second
after(1000, function() cat("Here I am!\n"))
# supplying arguments
x <- "print me!"
after(1000, function(x) print(x), args = list(x))
# we can remove x now, it is already stored in the timer
rm(x)
# calling functions in packages
# Instead of after(1000, utils::alarm) use
after(1000, function() utils::alarm())
# in case utils::alarm() uses other functions from the
# utils package.
# repeat a task
x <- after(1000, function() print("still here"), redo = 5)
Sys.sleep(3)
# list tasks
after$list()
# cancel a task
after$cancel(x)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.