rstudio / config

config package for R
https://rstudio.github.io/config/
256 stars 27 forks source link

Adds ability to use previously assigned parameters in R code. #35

Closed dermcnor closed 3 years ago

dermcnor commented 3 years ago

Creates new environment with all directly assigned parameters prior to evaluating R code. Fixes #9

andrie commented 3 years ago

Thanks for contributing this code.

After some testing, I found that the current implementation causes problems with nested list with ! expr statements. For example, consider this config.yml fragment:

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 = "-")

And then run this expectation:

  expect_identical(
    config::get("new_nested_not_found", config = "assigned"),
    "cyan"
  )

This fails as follows:

config::get("new_nested_not_found", config = "assigned") not identical to "cyan".
1/1 mismatches
x[1]: "paste(\"cyan\")-orange"
y[1]: "cyan"

Note how the expression has now been coerced into a character, and you end up with "paste(\"cyan\")-orange" as the value.

andrie commented 3 years ago

I have created a new branch called 35-list2env that contains your ideas and my own workings on top.

It may be simpler to continue experimenting in that branch.

Some of the changes include:

eval_fun <- function(expr, envir) {
    tryCatch(
      eval(expr, envir = envir),
      error = function(e) {
        eval_issues <<- append(
          eval_issues,
          paste(deparse(e$call), e$message, sep = ": ")
        )
        NULL
      })
  }

In addition, slightly modified message reporting.