[R] scoping problem?

Jonathan Qiang Li jonqli at labs.agilent.com
Tue Jul 17 16:01:59 CEST 2001


Hi, 

I wrote a dummy function that passes a dataset to tree() as the
following:

test.func <- function(training.data){
	tr <- tree(type~., data = training.data)
	tr
}

Then I run the function under R

> test.func(MyData)

Error in model.frame.default(formula = type ~ ., data = training.data) : 
	Object "training.data" not found

MyData resides in the working environment, while training.data does not.
R manual
claims that scoping of free variables are "lexical" (Introduction to R,
chapter 10.7).
 I take that it means that in evaluating the expression 
tree(type~., data=training.data) ,
R will look in the scope of the function test.func, therefore
discovering "training.data" 
as a "formal parameter". But it seemed that I misunderstood something
here. 

I also made a global variable 

> training.data <- MyData

and run 

> test.func(MyData) 

The function works fine without complaints.

Can someone please help with this matter?

Jonathan
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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