Last active
May 24, 2021 14:31
-
-
Save richfitz/ab50e44ad525a59f78d260831b8ed5b5 to your computer and use it in GitHub Desktop.
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
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