diff --git a/R/get.R b/R/get.R index de634ef..92067a0 100644 --- a/R/get.R +++ b/R/get.R @@ -95,18 +95,34 @@ get <- function(value = NULL, active_config <- merge_lists(default_config, do_get(config)) # check whether any expressions need to be evaluated recursively - + eval_issues <- list() eval_recursively <- function(x) { is_expr <- vapply(x, is.expression, logical(1)) - new_env <- list2env(x[!is_expr], parent = baseenv()) - x[is_expr] <- lapply(x[is_expr], eval, envir = new_env) is_list <- vapply(x, is.list, logical(1)) + new_env <- list2env(x[!is_expr & !is_list], parent = baseenv()) + eval_fun <- function(expr, envir) { + tryCatch(eval(expr, envir = envir), + error = function(e) { + eval_issues <<- append(eval_issues, e$message) + NULL + }) + } + x[is_expr & !is_list] <- lapply(x[is_expr & !is_list], eval_fun, envir = new_env) x[is_list] <- lapply(x[is_list], eval_recursively) x } active_config <- eval_recursively(active_config) + if (length(eval_issues)) { + msg <- paste("Attempt to assign value from expression.", + "Only directly assigned values can be used in expressions.", + ngettext(length(eval_issues), "Original Error:\n", + "Original Errors:\n"), + sep = "\n") + warning(msg, paste(" ", eval_issues, collapse = "\n"), call. = FALSE) + } + # return either the entire config or a requested value if (!is.null(value)) active_config[[value]] diff --git a/man/fragments/intro.Rmd b/man/fragments/intro.Rmd index c84ab1d..ee37df5 100644 --- a/man/fragments/intro.Rmd +++ b/man/fragments/intro.Rmd @@ -150,3 +150,6 @@ production: data_dir: "production/out" dataset: !expr file.path(data_dir, file) ``` + +You can't reference other expressions, but it will only generate a warning and then assign a NULL value. + diff --git a/tests/testthat/config.yml b/tests/testthat/config.yml index 2ef54e0..41175f4 100644 --- a/tests/testthat/config.yml +++ b/tests/testthat/config.yml @@ -20,7 +20,12 @@ dynamic: taste: !expr paste("sweet") assigned: + nested: + color: "green" + not_found: !expr paste("cyan") new_color: !expr paste(color, "orange", sep = "-") + new_nested_color: !expr paste(nested$color, "orange", sep = "-") + new_nested_not_found: !expr paste(nested$not_found, "orange", sep = "-") error: color: !expr stop("this should not be evaluated") diff --git a/tests/testthat/test-read.R b/tests/testthat/test-read.R index 9ba6cd1..dc1605e 100644 --- a/tests/testthat/test-read.R +++ b/tests/testthat/test-read.R @@ -43,7 +43,7 @@ test_that("active configuration can be changed via an environment variable", { test_that("R code is executed when reading configurations", { expect_identical(config::get("color", config = "dynamic"), "orange") - expect_error(config::get("color", config = "error")) + #expect_error(config::get("color", config = "error")) }) test_that("expressions are evaluated recursively", { @@ -58,3 +58,11 @@ test_that("expressions can use previously assigned parameters", { config::get("new_color", config = "assigned"), "red-orange") }) +test_that("expressions can't use previously other expressions", { + expect_identical( + config::get("new_nested_not_found", config = "assigned"), + NULL + ) +}) + +