R: Source personal scripts keeping some functions

2019-09-14 04:23发布

问题:

Follow up to this

I want to source scripts inside a given environment, like in sys.source, but "exporting" only some functions and keeping the others private.

I created this function:

source2=function(script){ 
    ps=paste0(script, "_")
    assign(ps, new.env(parent=baseenv()))
    assign(script, new.env(parent=get(ps)))    
    private=function(f){
        fn=deparse(substitute(f))
        assign(fn, f, parent.env(parent.frame()))
        rm(list=fn, envir=parent.frame())
    }
    assign("private", private, get(script))
    sys.source(paste0(script, ".R"), envir=get(script))
    rm(private, envir=get(script))
    attach(get(script), name=script)
}

For the most part, this function works as expected.
Consider the script:

## foo.R
f=function() g()
g=function() print('hello')
private(g)

Note the private() function, which will hide g().

If I, so to say, import the module foo:

source2("foo")

I have a new environment in the search path:

search()
##  [1] ".GlobalEnv"        "foo"               "package:stats"    
##  [4] "package:graphics"  "package:grDevices" "package:utils"    
##  [7] "package:datasets"  "package:methods"   "Autoloads"        
## [10] "package:base"     

The current environment, .GlobalEnv, shows only:

ls()
## [1] "source2"

But if I list items in foo environment:

ls("foo")
## [1] "f"

Therefore I can run:

f()
## [1] "hello"

The problem is that g() is hidden totally.

getAnywhere(g)
## no object named 'g' was found

Too much. In fact, if I want to debug f():

debug(f)
f()
debugging in: f()
## Error in f() : could not find function "g"

The question is:
Where is g()? Can I still retrieve it?

回答1:

Use:

get("g",env=environment(f))
## function ()
## print("hello")
## <environment: 0x0000000018780c30>

ls(parent.env(environment(f)))
## [1] "g"

Credit goes to Alexander Griffith for the solution.