Last active
February 24, 2021 04:10
-
-
Save debruine/7c39ba6ae89f1059d164572a67f6e04e to your computer and use it in GitHub Desktop.
Set the default values from a function to the global environment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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