Skip to content

Instantly share code, notes, and snippets.

@moritzpschwarz
Last active August 11, 2020 14:11
Show Gist options
  • Save moritzpschwarz/686a1acef209a534d0f7b51b4fd9adc1 to your computer and use it in GitHub Desktop.
Save moritzpschwarz/686a1acef209a534d0f7b51b4fd9adc1 to your computer and use it in GitHub Desktop.
This gist is supposed to quickly copy all formal arguments to the Global Environment. This makes inspecting a function much easier (although I'm sure there are many easier ways to do this).
# Currently this is implemented for numeric vectors (e.g. c(1,2) and c(1:5)), character vectors, lists and logical arguments.
# Further work might be needed, depending on the function arguments
extract_function_arguments <- function(myfunction) {
obj <- as.list(formals(myfunction))
lapply(seq_along(obj), function(i) {
assign(names(obj)[i],
if (class(obj[[i]]) == "call") {
if (all(as.character(obj[[i]])[-1] %in% c(0:9))) {
if (as.character(obj[[i]])[1] == ":") {
as.numeric(as.character(obj[[i]])[2]):as.numeric(as.character(obj[[i]])[3])
} else {
as.numeric(as.character(obj[[i]]))
}
} else {
if (as.character(obj[[i]])[1] == "list") {
as.list(as.character(obj[[i]])[-1])
} else {
as.character(obj[[i]])[-1]
}
}
} else {
obj[[i]]
},
envir = .GlobalEnv
)
})
}
# the example used here uses the getsm function from the gets package
library(gets)
extract_function_arguments(getsm)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment