Skip to content

Instantly share code, notes, and snippets.

@degaray
Last active September 4, 2015 02:04
Show Gist options
  • Save degaray/32afcf6cfd34b2da26c0 to your computer and use it in GitHub Desktop.
Save degaray/32afcf6cfd34b2da26c0 to your computer and use it in GitHub Desktop.
Obtain all the undefined variables in an expression
find_unbound <- function(lang) {
stopifnot(is.language(lang), !is.expression(lang))
bound <- character()
unbound <- character()
rec_fun <- function(lang) {
if(is.call(lang)) {
# These are assignment calls; if any symbols are assigned and have
# not already been found in a leaf, they are defined as bound
if((lang[[1]] == as.name("<-") || lang[[1]] == as.name("="))) {
for(i in as.list(lang)[-(1:2)]) Recall(i)
if(is.name(lang[[2]]) && !as.character(lang[[2]]) %in% unbound)
bound <<- c(bound, as.character(lang[[2]]))
} else for(i in as.list(lang)[-1]) Recall(i)
} else if (is.name(lang) && ! as.character(lang) %in% bound)
# this is a leaf; any previously bound symbols are by definition
# unbound
unbound <<- c(unbound, as.character(lang))
}
rec_fun(lang)
unique(unbound)
}
file_unbound(lang)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment