Last active
February 23, 2024 13:39
-
-
Save alekrutkowski/06fab5fe365e43aa6d403da6d0127ec1 to your computer and use it in GitHub Desktop.
async/non-blocking programming in R with `mirai`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Usage: | |
# a = 1; b = 2 | |
# my_result <- eval.async(a+b) | |
### when mirai::unresolved(my_result) == FALSE | |
### you can use my_result$data | |
### do.call.async can be used like do.call | |
eval.async <- | |
function(.expr, | |
.list_of_objects = mget(ls(envir=parent.frame(),all.names=TRUE), | |
envir=parent.frame()), | |
.loaded_packages = sub("package:", "", | |
grep("package:",search(),value=TRUE), | |
fixed=TRUE)) { | |
force(.list_of_objects) | |
eval(bquote(mirai::mirai({ | |
local(for (package in .(.loaded_packages)) | |
library(package, character.only = TRUE)) | |
list2env(.(.list_of_objects), envir = .GlobalEnv) | |
.(substitute(.expr)) | |
}))) | |
} | |
do.call.async <- function(what, args) | |
eval(bquote(eval.async(do.call(.(what),.(args))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment