Title: | Run Code as an RStudio Job - Free Your Console |
---|---|
Description: | Call job::job({<code here>}) to run R code as an RStudio job and keep your console free in the meantime. This allows for a productive workflow while testing (multiple) long-running chunks of code. It can also be used to organize results using the RStudio Jobs GUI or to test code in a clean environment. Two RStudio Addins can be used to run selected code as a job. |
Authors: | Jonas Kristoffer Lindeløv [aut, cre]
|
Maintainer: | Jonas Kristoffer Lindeløv <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.3.1 |
Built: | 2025-01-31 05:24:15 UTC |
Source: | https://github.com/lindeloev/job |
Call this function as the last line in job::job()
to select what is exported
back into globalenv()
. export()
does nothing if called in any other context.
export(value = "changed", file = NULL)
export(value = "changed", file = NULL)
value |
What to return. One of:
|
file |
Name of |
Under the hood, this function merely rm()
variables that does not match value
.
Because job::job()
returns everything at the end of the script, this defines
what is returned.
NULL
invisibly.
Jonas Kristoffer Lindeløv, [email protected]
if (rstudioapi::isAvailable()) { a = 55 b = 77 d = 88 job::job({n = 11; a = 55; job::export("all")}) # export a, b, d, n job::job({n = 11; a = 11; job::export("changed")}) # export a, n job::job({n = 11; a = 11; job::export("new")}) # export n job::job({n = 11; a = 55; job::export(c(a, d, b))}) # export a, d, b job::job({n = 11; a = 55; job::export("none")}) # export nothing # To file job::job({n = 11; a = 11; job::export("changed", file = "jobresult.RData")}) # save a, n jobresult = new.env() # import to this env instead of global load("jobresult.RData", envir = jobresult) print(jobresult$n) }
if (rstudioapi::isAvailable()) { a = 55 b = 77 d = 88 job::job({n = 11; a = 55; job::export("all")}) # export a, b, d, n job::job({n = 11; a = 11; job::export("changed")}) # export a, n job::job({n = 11; a = 11; job::export("new")}) # export n job::job({n = 11; a = 55; job::export(c(a, d, b))}) # export a, d, b job::job({n = 11; a = 55; job::export("none")}) # export nothing # To file job::job({n = 11; a = 11; job::export("changed", file = "jobresult.RData")}) # save a, n jobresult = new.env() # import to this env instead of global load("jobresult.RData", envir = jobresult) print(jobresult$n) }
See examples for an introduction. See the job website for more examples.
See details for some warnings.
Note that job::empty()
is identical to job::job()
but all arguments default to NULL
.
job( ..., import = "all", packages = .packages(), opts = options(), title = NULL ) empty(..., import = NULL, packages = NULL, opts = NULL, title = NULL)
job( ..., import = "all", packages = .packages(), opts = options(), title = NULL ) empty(..., import = NULL, packages = NULL, opts = NULL, title = NULL)
... |
A named or unnamed code block enclosed in curly brackets, |
import |
Which objects to import into the job.
|
packages |
Character vector of packages to load in the job. Defaults to
all loaded packages in the calling environment. |
opts |
List of options to overwrite in the job. Defaults to |
title |
The job title. You can write e.g., |
This is a wrapper around rstudioapi::jobRunScript
. To control what gets
returned, see export
. By default, all objects that changed during
the job are returned, i.e., job::export("changed")
.
Returning large objects:jobRunScript
is very
slow at importing and exporting large objects. For exporting back into
globalenv()
, it may be faster to saveRDS()
results within the job and
readRDS()
them in your environment.
Invisibly returns the job id on which you can call other rstudioapi::job*
functions, e.g., rstudioapi::rstudioapi::jobRemove(job_id)
.
empty()
: job::job()
but with NULL defaults, i.e., an "empty" job.
Jonas Kristoffer Lindeløv, [email protected]
if (rstudioapi::isAvailable()) { # Unnamed code chunks returns to globalenv() global_var = 5 job::job({ x = rnorm(global_var) print("This text goes to the job console") m = mean(x) }) # later: print(x) print(m) # Named code chunks assign job environment to that name job::job(my_result = { y = rnorm(global_var) sigma = sd(y) }, title = "Title with code: {code}") # later: print(my_result$y) print(my_result$sigma) # Delete everything in the job environment to return nothing. # Useful if text output + file output is primary job::job({ some_cars = mtcars[mtcars$cyl > 4, ] print(mean(some_cars$mpg)) print(summary(some_cars)) # saveRDS(some_cars, "job_result.rds") job::export("none") # return nothing }) # Control imports from calling environment (variables, packages, options) my_df = data.frame(names = c("alice", "bob")) ignore_var = 15 job::job(result2 = { if (exists("ignore_var") == FALSE) print("ignore_var is not set here") names = rep(my_df$names, global_var) }, import = c(global_var, my_df), packages = NULL, opts = list(mc.cores = 3)) # later print(result2$names) }
if (rstudioapi::isAvailable()) { # Unnamed code chunks returns to globalenv() global_var = 5 job::job({ x = rnorm(global_var) print("This text goes to the job console") m = mean(x) }) # later: print(x) print(m) # Named code chunks assign job environment to that name job::job(my_result = { y = rnorm(global_var) sigma = sd(y) }, title = "Title with code: {code}") # later: print(my_result$y) print(my_result$sigma) # Delete everything in the job environment to return nothing. # Useful if text output + file output is primary job::job({ some_cars = mtcars[mtcars$cyl > 4, ] print(mean(some_cars$mpg)) print(summary(some_cars)) # saveRDS(some_cars, "job_result.rds") job::export("none") # return nothing }) # Control imports from calling environment (variables, packages, options) my_df = data.frame(names = c("alice", "bob")) ignore_var = 15 job::job(result2 = { if (exists("ignore_var") == FALSE) print("ignore_var is not set here") names = rep(my_df$names, global_var) }, import = c(global_var, my_df), packages = NULL, opts = list(mc.cores = 3)) # later print(result2$names) }
Nice print .jobcode
## S3 method for class 'jobcode' print(x, ...)
## S3 method for class 'jobcode' print(x, ...)
x |
Text to print |
... |
Currently unused |
No return value, called for side effects.