Skip to content
Prev 12385 / 398502 Next

scoping problem?

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
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
and run
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._