[R] all.vars for nested expressions

William Dunlap wdunlap at tibco.com
Mon Apr 29 16:59:16 CEST 2013


Try poking around in the codetools package.  E.g., you can do things like the following

   expr1 <- quote(a <- fn + tp) # put 'a' in the expression
   expr2 <- quote( tp / a + fn)
   expr12 <- call("{", expr1, expr2)
   expr12
   # {
   #    a <- fn + tp
   #    tp/a + fn
   # }
   library(codetools)
   findLocals(expr12) # from codetools
   # [1] "a"
   setdiff(all.vars(expr12), findLocals(expr12))
   # [1] "fn" "tp"

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of flxms
> Sent: Monday, April 29, 2013 4:34 AM
> To: r-help at r-project.org
> Subject: [R] all.vars for nested expressions
> 
> Dear R fellows,
> 
> Assume I define
> 
> a <- expression(fn+tp)
> sen <- expression(tp/a)
> 
> Now I'd like to know, which variables are necessary for calculating sen
> 
> all.vars(sen)
> 
> This results in a vector c(tp,a). But I'd like all.vars to evaluate the
> sen-object down to the ground level, which would result in a vector
> c(tp,fn) (because a was defined as fn+tp). In other words, I'd like
> all.vars to expand the a-object (and all other downstream objects). I am
> looking for a solution, that works with much more levels. This is just a
> very simple example.
> 
> I'd appreciate any suggestions how to do that very much!
> Thanks in advance,
> 
> Felix
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at 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.



More information about the R-help mailing list