[R] scoping problems

Heberto Ghezzo Heberto at meakins.lan.mcgill.ca
Thu Jul 13 23:24:41 CEST 2000


Again the same type of problem with scoping.
I wrote the simple functions below, 'by' is from Trevor Hastie 
modified by F.Harrel and copied from the list.

epi.crosstab <- function(vec1, vec2, vec3=NULL, row.labels = 
NULL, col.labels = NULL)
{
   if(!is.null(vec3) ){
         data <- eval(epi.file,sys.frame(sys.parent()))
         by(vec3, xtab(vec1,vec2, row.labels = row.labels, col.labels = 
                col.labels),data=data)
     }
    if(is.null(vec3))
        xtab(vec1, vec2, row.labels = row.labels, col.labels=               
               col.labels)
}
 
by <- function(group, exp, data=data)
{
   G <- substitute(group)
   exp <- substitute(exp)
   G <- factor(eval(G))
   for(group in levels(G)) {
       eval(exp, envir=c(data[G == group, ]))
   }
   invisible()
}

xtab <- function(vector1, vector2,row.labels=NULL,col.labels=NULL)
{
   table(vector1, vector2)
}

now I have a data.frame that is always called 'epi.file' and is 
attached to frame 1, MILK, COFFEE and WATER are variables in 
the frame

> epi.crosstab(MILK,COFFEE)
       vector2
vector1  N  Y
      N 44 27
      Y  0  4
==Why it does print vector1, vector 2 and not MILK and COFFEE ?

> epi.crosstab(MILK,COFFEE,WATER)
Error in eval(expr, envir, enclos) : Object "vec3" not found
> 

Can somebody be so kind as to try to explain which scoping rule 
now I did violate and where should I put 'eval' 'substitute' etc so it 
works.
Thanks
.
R. Heberto Ghezzo  Ph.D.
Meakins-Christie Labs
McGill University
Montreal - Canada
heberto at meakins.lan.mcgill.ca
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list