[R] all.vars for nested expressions

Eik Vettorazzi E.Vettorazzi at uke.de
Mon Apr 29 16:45:58 CEST 2013


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



More information about the R-help mailing list