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]]
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]]
Hi Felix, I thought, this could be an easy task for substitute, and the following works as expected: all.vars(substitute(expression(tp/a),list(a=expression(fn+tp)))) # [1] "tp" "fn" But (of course) all.vars(substitute(sen,list(a=a))) does not yield the desired result, and I can't figure out, how to set up as.name, bquote, eval, deparse etc to do the task properly. Instead, my approach is a recursive call to all.vars xall.help<-function(x){ #check if there is an object with name x if(exists(x)) lapply(all.vars(get(x)),xall.help) else x} xall.vars<-function(x){ if (!is.character(x)) x<-paste(substitute(x)) #for convenience put in a single vecotr #xall.help returns a 'parsed tree' unique(unlist(xall.help(x))) } #example fn<-expression(n1+n2) a <- expression(fn+tp) sen <- expression(tp/a) xall.vars(sen) # [1] "tp" "n1" "n2" cheers. Am 29.04.2013 13:33, schrieb flxms:> 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. >-- Eik Vettorazzi Institut f?r Medizinische Biometrie und Epidemiologie Universit?tsklinikum Hamburg-Eppendorf Martinistr. 52 20246 Hamburg T ++49/40/7410-58243 F ++49/40/7410-57790 -- Pflichtangaben gem?? Gesetz ?ber elektronische Handelsregister und Genossenschaftsregister sowie das Unternehmensregister (EHUG): Universit?tsklinikum Hamburg-Eppendorf; K?rperschaft des ?ffentlichen Rechts; Gerichtsstand: Hamburg Vorstandsmitglieder: Prof. Dr. Martin Zeitz (Vorsitzender), Dr. Alexander Kirstein, Joachim Pr?l?, Prof. Dr. Dr. Uwe Koch-Gromus
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.