Thanks to Mark for an off-list suggestion which works.

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

this apparently defines a copy of thefunction within somefunction, exactly
what I wanted. thefunction in the global env is unchanged.

It seems so obvious, but it would have been alot more head scratching. The
help is very much appreciated!

James

On Thu, May 26, 2011 at 3:09 PM, James McCreight <jlmccrei...@gmail.com>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.
>
> thanks!!
>
> James
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to