Skip to content

Instantly share code, notes, and snippets.

@richfitz
Last active May 24, 2021 14:31
Show Gist options
  • Save richfitz/ab50e44ad525a59f78d260831b8ed5b5 to your computer and use it in GitHub Desktop.
Save richfitz/ab50e44ad525a59f78d260831b8ed5b5 to your computer and use it in GitHub Desktop.
find_symbols <- function(expr) {
functions <- variables <- character(0)
f <- function(e) {
if (!is.recursive(e)) {
if (!is.symbol(e)) { # A literal of some type
return()
}
variables <<- c(variables, deparse(e))
} else {
functions <<- c(functions, deparse(e[[1]]))
for (a in as.list(e[-1])) {
if (!missing(a)) {
f(a)
}
}
}
}
if (inherits(expr, "formula")) {
expr <- expr[[length(expr)]]
}
f(expr)
list(functions=unique(functions),
variables=unique(variables))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment