Re: [R] source a specific function

From: Duncan Murdoch <>
Date: Mon, 18 Jun 2007 14:53:50 -0400

On 6/18/2007 12:11 PM, (Ted Harding) wrote:
> On 18-Jun-07 14:28:35, Gabor Grothendieck wrote:

>> This loads all the functions into an anonymous environment defined
>> by local and then exports f to the global environment.
>> f <- local({
>>       source("/a.R", local = TRUE)
>>       environment(f) <- .GlobalEnv
>>       f
>> })

> That looks neat! Two questions:
> 1. Would something similar work for extracting selected functions
> from a library (assuming that you know about interdependencies)?
> E.g. something like
> f <- local({
> library(f.etc.lib)
> environment(f) <- .GlobalEnv
> f
> })

The exact syntax you list there won't work, but in any case, changing the environment of a function in a package is a bad idea -- it may need to reference things from the namespace of the package.

> 2. Having done what you describe to extract just f from a source
> file, can one then "delete" the local environment used to load
> the source? I think what I'm basically asking is whether the
> exporting is done "by value" (local environment deletion OK)
> or "by reference" (deletion would destroy the exported object).

Gabor answered this: it will go away automatically.

Duncan Murdoch mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Mon 18 Jun 2007 - 18:59:30 GMT

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 Mon 18 Jun 2007 - 22:32:21 GMT.

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