Skip to content

Instantly share code, notes, and snippets.

@jbryer
Created May 5, 2026 14:01
Show Gist options
  • Select an option

  • Save jbryer/76a333d75a0b3d1118ba42f969dd6156 to your computer and use it in GitHub Desktop.

Select an option

Save jbryer/76a333d75a0b3d1118ba42f969dd6156 to your computer and use it in GitHub Desktop.
Set R function parameters to an environment
#' Set function parameters to an environment.
#'
#' This function is designed to help debug functions. It will attempt to set all
#' the default parameter values to the specified environment (global environment
#' by default). This is useful for when you want to execute code within the
#' function definition interactively but need the parameters set in the current
#' environment.
#'
#' **Warning:** This function will modify the global environment and therefore
#' violates CRAN policy
#' ["Packages should not modify the global environment (user’s workspace)"]
#' (https://cran.r-project.org/web/packages/policies.html#Source-packages).
#'
#' @param FUN the function to assign parameters to an environment.
#' @param envir the environment to assign the variables to. Defaults to the
#' global environment.
#' @param verbose whether to return the data frame invisibly or to print the results.
#' @return a data frame where row names correspond to the parameter name with
#' two columns: `set` which is logical indicating if the variable was set
#' and `value` with a character representation of the variable value.
set_function_params <- function(FUN, envir = globalenv(), verbose = interactive()) {
params <- formals(FUN)
params_set <- data.frame(row.names = names(params),
set = rep(FALSE, length(params)),
value = rep(NA_character_, length(params)))
for(param in names(params)) {
value <- params[[param]]
if(!missing(value)) {
if(is.character(value)) {
assign(param, value, envir = envir)
params_set[param,]$value <- value
} else {
assign(param, eval(value), envir = envir)
params_set[param,]$value <- eval(value)
}
params_set[param,]$set <- TRUE
}
}
if(verbose) {
return(params_set)
} else {
invisible(params_set)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment