Re: [R] change function scope?

From: James McCreight <>
Date: Thu, 26 May 2011 17:38:43 -0600

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!


On Thu, May 26, 2011 at 3: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.
> thanks!!
> James

        [[alternative HTML version deleted]] mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Thu 26 May 2011 - 23:40:32 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 26 May 2011 - 23:50:10 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive