Skip to content

Instantly share code, notes, and snippets.

@debruine
Last active February 24, 2021 04:10
Show Gist options
  • Save debruine/7c39ba6ae89f1059d164572a67f6e04e to your computer and use it in GitHub Desktop.
Save debruine/7c39ba6ae89f1059d164572a67f6e04e to your computer and use it in GitHub Desktop.
Set the default values from a function to the global environment
#' Global Arguments
#'
#' Set the default values from a function to the global environment
#'
#' @param func the function to get arguments from
#' @param env the environment to out the arguments in
#'
#' @return NULL
#' @export
#'
#' @examples
#' global_args(rnorm)
global_args <- function(func, env = .GlobalEnv) {
# get the arguments and their defaults
a <- as.list(args(func))
# get rid of symbols
sym <- sapply(a, is.symbol)
a[sym] <- NULL
# get rid of last null
a[names(a) == ""] <- NULL
# eval language
lang <- sapply(a, is.language)
a[lang] <- sapply(a[lang], eval)
# put in env
list2env(a, env)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment