Last active
September 4, 2015 02:04
-
-
Save degaray/32afcf6cfd34b2da26c0 to your computer and use it in GitHub Desktop.
Obtain all the undefined variables in an expression
This file contains hidden or 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_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
As defined in this StackOverflow question: http://stackoverflow.com/questions/32363338/how-to-obtain-all-the-variables-that-need-to-be-defined-prior-evaluating-an-expr/32365609#32365609