On 11-05-26 5:09 PM, James McCreight wrote:
I'm still getting used to R's scoping. I've run into the following situation

value=0
thefunction<- function() print( value )
somefunction<- function() { value=99; thefunction() }
somefunction()

now, I understand that somefunction() returns 0 because thefunction() was
defined with value=0 in its parent envrionment, it dosent look at all in the
environment of somefunction. My question is can I re-scope thefunction in
the somefunction environment?? (So that somefunction() returns 99). I've
tried various uses of eval() and assign() with their keywords in the
definition of somefunction and have had no luck at all.

I do see that i could define thefunction in some external file and source it
inside somefunction, but that seems clumsy.


Yes, you've been told how to do it: but you almost certainly shouldn't. Think of functions as functions, not as macros. Changing the environment of a function is like editing it. Would you ask how to change the body of thefunction from within somefunction? You can in R, but you shouldn't.

Duncan Murdoch

______________________________________________
[email protected] 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.

Reply via email to