Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: set_config() invisibly returns prior values like options() #11

Open
rossellhayes opened this issue Sep 5, 2020 · 0 comments
Labels
feature a feature request or enhancement

Comments

@rossellhayes
Copy link

rossellhayes commented Sep 5, 2020

options() invisibly returns its prior value. This can be helpful for use in a single test or function, with lines like:

op <- options(scipen = 1000)
on.exit(options(op), add = TRUE)

set_config() could benefit from being able to use a similar workflow. The most obvious case would be in unit testing, where this could aid cleanup. It could also be useful when one function requires a different configuration than the rest of the package.

In my own package, I use the following code to achieve this:

set_config <- function (...) {
  input <- list(...)
  if (is.null(names(input))) {input <- unlist(input, recursive = FALSE)}

  if (is.null(names(input)) || any(names(input) == "")) {
    stop("Some parameters are not named")
  }
  
  values        <- lapply(names(input), pkgconfig::get_config)
  names(values) <- names(input)
  
  do.call(pkgconfig::set_config_in, c(input, .in = parent.frame()))
  
  return(invisible(values))
}

x <- set_config("foo" = "bar")
on.exit(set_config(x), add = TRUE)

It's certainly not the most elegant, but it seems to get the job done.

@gaborcsardi gaborcsardi added the feature a feature request or enhancement label Oct 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants