Created
May 17, 2019 18:44
-
-
Save eliocamp/747ca3f3499368858232045add08d927 to your computer and use it in GitHub Desktop.
Evaluates expressions in an RStudio Job
This file contains hidden or 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: | |
| # variable %<j% long_running_function() | |
| `%<j%` <- function(x, y) { | |
| var_name <- paste0(deparse(substitute(x))) | |
| expr <- paste0(deparse(substitute(y)), collapse = "\n") | |
| script <- tempfile() | |
| lines <- writeLines(paste0(var_name, " <- ", expr), script) | |
| rstudioapi::jobRunScript(script, | |
| name = paste0("computing ", var_name), | |
| workingDir = getwd(), | |
| importEnv = TRUE, | |
| exportEnv = "R_GlobalEnv") | |
| } | |
| # Usage: | |
| # with_job(long_running_function(), "variable") | |
| with_job <- function(expr, var_name) { | |
| expr <- paste0(deparse(substitute(expr)), collapse = "\n") | |
| script <- tempfile() | |
| lines <- writeLines(paste0(var_name, " <- ", expr), script) | |
| rstudioapi::jobRunScript(script, | |
| name = paste0("computing ", var_name), | |
| workingDir = getwd(), | |
| importEnv = TRUE, | |
| exportEnv = "R_GlobalEnv") | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment