Thanks. That works if I a have the formula expression handy. But suppose I want a function, findGlobalVars() that takes a function as an argument and finds globals in it, where I have absolutely no idea what is in the supplied function:
findGlobalVars <- function(f) { require(codetools) findGlobals(f, merge=FALSE)$variables } findGlobalVars(plotFn1) I would like findGlobalVars() to be able to find variables in formulae that might be present in f. On 11/1/12 1:19 PM, "Bert Gunter" <gunter.ber...@gene.com> wrote: >Does > >?all.vars >##as in >> all.vars(y~x) >[1] "y" "x" > >help? > >-- Bert > >On Thu, Nov 1, 2012 at 11:04 AM, Hafen, Ryan P <ryan.ha...@pnnl.gov> >wrote: >> I need to find all global variables being used in a function and >>findGlobals() in the codetools package works quite nicely. However, I >>am not able to find variables that are used in formulae. Simply >>avoiding formulae in functions is not an option because I do not have >>control over what functions this will be applied to. >> >> Here is an example to illustrate: >> >> library(codetools) >> >> xGlobal <- rnorm(10) >> yGlobal <- rnorm(10) >> >> plotFn1 <- function() { >> plot(yGlobal ~ xGlobal) >> } >> >> plotFn2 <- function() { >> y <- yGlobal >> x <- xGlobal >> plot(y ~ x) >> } >> >> plotFn3 <- function() { >> plot(xGlobal, yGlobal) >> } >> >> findGlobals(plotFn1, merge=FALSE)$variables >> # character(0) >> findGlobals(plotFn2, merge=FALSE)$variables >> # [1] "xGlobal" "yGlobal" >> findGlobals(plotFn3, merge=FALSE)$variables >> # [1] "xGlobal" "yGlobal" >> >> I would like to find that plotFn1 also uses globals xGlobal and >>yGlobal. Any suggestions on how I might do this? >> >> ______________________________________________ >> R-help@r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >>http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. > > > >-- > >Bert Gunter >Genentech Nonclinical Biostatistics > >Internal Contact Info: >Phone: 467-7374 >Website: >http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-bio >statistics/pdb-ncb-home.htm ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.