Skip to content

Instantly share code, notes, and snippets.

@eliocamp
Created May 17, 2019 18:44
Show Gist options
  • Select an option

  • Save eliocamp/747ca3f3499368858232045add08d927 to your computer and use it in GitHub Desktop.

Select an option

Save eliocamp/747ca3f3499368858232045add08d927 to your computer and use it in GitHub Desktop.
Evaluates expressions in an RStudio Job
# 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