Skip to content

Commit

Permalink
Accept job(..., import = c("quoted", "vars"))
Browse files Browse the repository at this point in the history
  • Loading branch information
lindeloev committed May 4, 2024
1 parent f755997 commit 64f2d86
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
6 changes: 3 additions & 3 deletions R/call_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ save_env = function(vars, env, code_str) {
vars = ls_varnames[ls_varnames %in% all.names(parse(text = code_str))]
} else if (length(vars) == 1 && vars == "all") {
vars = ls(envir = env, all.names = TRUE)
} else if (vars[1] == "c") {
vars = vars[-1]
} else if (is.character(vars)) {
# Stay character vector
} else {
stop("`import` must be one of 'all', 'auto', NULL, or a c(vector, of, variables).")
stop("`import` must be one of 'all', 'auto', NULL, c(unquoted, variables), or c('quoted', 'variables').")
}

# Show import size
Expand Down
10 changes: 5 additions & 5 deletions R/job.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#' * `"auto"` (default): Detect which objects are used in the code and import
#' those.
#' * `c(foo, bar, ...)`: A vector of unquoted variables to import into the job.
#' * `c("foo", "bar", ...)`: A vector of quoted variables to import into the job.
#' * `NULL`: import nothing.
#' @param packages Character vector of packages to load in the job. Defaults to
#' all loaded packages in the calling environment. `NULL` loads only default
Expand Down Expand Up @@ -161,11 +162,12 @@ job = function(..., import = "all", packages = .packages(), opts = options(), ti
##########
# IMPORT #
##########
if (is.call(import) == FALSE)
import = substitute(import)
# Handle quoted vars etc.
if (is.character(import) == FALSE)
import = as.character(substitute(import))[-1]

import_summary = save_env(
vars = as.character(import),
vars = import,
env = parent.frame(),
code_str = code_str
)
Expand All @@ -184,8 +186,6 @@ job = function(..., import = "all", packages = .packages(), opts = options(), ti
suppressWarnings(saveRDS(.__jobsettings__, .__jobsettings__$file, compress = FALSE)) # Ignore warning that some package may not be available when loading




########################
# BUILD R CODE FOR JOB #
########################
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ job::job({

# Continue working in your console
cat("I'm free now! Thank you.
Sincerely, Console.")
Yours truly, Console.")
```

Now you can follow the progress in the jobs pane and your console is free in the meantime.
Now you can follow the progress in the jobs pane and your console is free in the meantime.


## Tweak your job(s)
Expand Down
14 changes: 7 additions & 7 deletions tests/testthat/test_job.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ if (rstudioapi::isAvailable()) {



#############
# TEST ARGS #
#############
######################
# TEST UNQUOTED ARGS #
######################
test_that("Set arguments in job::job()", {
# Set env
a = 123
Expand Down Expand Up @@ -106,9 +106,9 @@ if (rstudioapi::isAvailable()) {



##############################
# TEST empty() WITH ARGS #
##############################
#################################
# TEST empty() WITH QUOTED ARGS #
#################################
test_that("Set arguments in job::empty()", {
# Set env
a = 123
Expand All @@ -124,7 +124,7 @@ if (rstudioapi::isAvailable()) {
attached_rstudioapi = exists("isAvailable")
opts = options()
print("output!")
}, import = c(b, q), packages = c("job"), title = "something weird: #/(¤", opts = list(job.newopt = 59))
}, import = c("b", "q"), packages = c("job"), title = "something weird: #/(¤", opts = list(job.newopt = 59))

# Check result
helpers$wait_for_job("with_args2")
Expand Down

0 comments on commit 64f2d86

Please sign in to comment.