Skip to content

Commit

Permalink
minor speedups
Browse files Browse the repository at this point in the history
  • Loading branch information
wlandau committed Aug 27, 2024
1 parent 254f0b2 commit e72c238
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 13 deletions.
9 changes: 8 additions & 1 deletion R/class_counter.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ counter_exists_name <- function(counter, name) {
}

counter_exist_names <- function(counter, names) {
unlist(lapply(names, exists, envir = counter$envir, inherits = FALSE))
as.logical(
lapply(
names,
exists,
envir = counter$envir,
inherits = FALSE
)
)
}

counter_filter_exists <- function(counter, names) {
Expand Down
2 changes: 1 addition & 1 deletion R/class_store_format_custom.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ store_format_custom_call_method <- function(store, text, args) {
do.call(what = Sys.setenv, args = as.list(envvars))
}
envir <- new.env(parent = baseenv())
what <- eval(parse(text = text), envir = envir)
what <- eval(parse(text = text, keep.source = FALSE), envir = envir)
do.call(what = what, args = args, envir = envir)
}

Expand Down
2 changes: 1 addition & 1 deletion R/class_store_repository_cas.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,6 @@ store_repository_cas_call_method <- function(store, text, args) {
do.call(what = Sys.setenv, args = as.list(envvars))
}
envir <- new.env(parent = baseenv())
what <- eval(parse(text = text), envir = envir)
what <- eval(parse(text = text, keep.source = FALSE), envir = envir)
do.call(what = what, args = args, envir = envir)
}
4 changes: 2 additions & 2 deletions R/tar_dir.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ tar_dir <- function(code) {
code <- substitute(code)
dir <- tempfile(pattern = "targets_")
dir_create(dir)
old <- eval(parse(text = "setwd(dir)"))
old <- eval(parse(text = "setwd(dir)", keep.source = FALSE))
on.exit({
eval(parse(text = "setwd(old)"))
eval(parse(text = "setwd(old)", keep.source = FALSE))
unlink(dir, recursive = TRUE)
})
eval(code, envir = parent.frame())
Expand Down
2 changes: 1 addition & 1 deletion R/tar_load_globals.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ tar_load_globals <- function(
) {
force(envir)
tar_assert_script(script)
eval(parse(file = script), envir = envir)
eval(parse(file = script, keep.source = TRUE), envir = envir)
map(
x = tar_option_get("packages"),
f = library,
Expand Down
5 changes: 4 additions & 1 deletion R/tar_make_interactive.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
#' message(x)
#' }
tar_make_interactive <- function(code) {
targets <- eval(parse(text = code), envir = tar_option_get("envir"))
targets <- eval(
parse(text = code, keep.source = TRUE),
envir = tar_option_get("envir")
)
pipeline <- pipeline_from_list(targets)
pipeline_reset_deployments(pipeline)
queue <- if_any(
Expand Down
4 changes: 2 additions & 2 deletions R/tar_source.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ tar_source <- function(
function(script) {
if (change_directory) {
dir <- dirname(script)
old <- eval(parse(text = "setwd(dir)"))
on.exit(eval(parse(text = "setwd(old)")))
old <- eval(parse(text = "setwd(dir)", keep.source = FALSE))
on.exit(eval(parse(text = "setwd(old)", keep.source = FALSE)))
expr <- parse(file = basename(script), keep.source = TRUE)
} else {
expr <- parse(file = script, keep.source = TRUE)
Expand Down
2 changes: 1 addition & 1 deletion R/tar_workspace.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ tar_workspace <- function(
workspace_load_packages(workspace)
}
if (source) {
eval(parse(text = readLines(script)), envir = envir)
eval(parse(text = readLines(script), keep.source = TRUE), envir = envir)
}
workspace_set_seed(workspace)
invisible()
Expand Down
2 changes: 1 addition & 1 deletion R/utils_assert.R
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ tar_assert_script <- function(script) {
"Functions tar_edit() and tar_script() can help. "
)
tar_assert_path(script, msg)
vars <- all.vars(parse(file = script), functions = TRUE)
vars <- all.vars(parse(file = script, keep.source = TRUE), functions = TRUE)
exclude <- c(
"glimpse",
"make",
Expand Down
10 changes: 8 additions & 2 deletions R/utils_script.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ tar_script_options <- function(script) {
# Covered in unit tests but runs in a different R process.
# nocov start
func = function(script) {
eval(parse(file = script), envir = targets::tar_option_get("envir"))
eval(
parse(file = script, keep.source = TRUE),
envir = targets::tar_option_get("envir")
)
targets::tar_option_export()
},
# nocov end
Expand All @@ -27,7 +30,10 @@ tar_script_targets <- function(script) {
# Covered in unit tests but runs in a different R process.
# nocov start
func = function(script) {
eval(parse(file = script), envir = targets::tar_option_get("envir"))
eval(
parse(file = script, keep.source = TRUE),
envir = targets::tar_option_get("envir")
)
},
# nocov end
args = list(script = script)
Expand Down

0 comments on commit e72c238

Please sign in to comment.