mikeck mikeck - 14 days ago 6
R Question

exposing functions without cluttering ls()

I have a package that generates some functions when you call an

initialize
function. I create these functions in the
parent.frame
of
initialize()
, which I guess is the global environment. I want to emulate the normal package behavior that allows you to directly call a function from a package after loading it, but without having to see those functions when you list your workspace contents using
ls()
. For example, doing

library(ggplot2)
ls()


doesn't return
geom_line
,
geom_point
, etc., but you don't have to use
::
to call those functions. They are exposed to the user but do not live in the global environment.

Is there a clever way for me to do the same thing for functions generated by the call to
initialize
, e.g. by defining environments or namespaces in
zzz.r
and the
onLoad
or
onAttach
hooks? I thought of trying to set the function environments to the package namespace, but it seems that you cannot modify the package namespace after it is loaded.

EDIT the package I'm working on is here: https://github.com/mkoohafkan/arcpyr. The
arcpy.initialize
function connects to Python using PythonInR, imports the
arcpy
package, and then creates interfaces for a list of functions. I'll try to create a simplified dummy package later today.

Answer

So I eventually found a solution that uses both environments (thanks @ssdecontrol!) and attach.

f = new.env()                   # create the environment f
assign("foo", "bar", pos = f)   # create the variable foo inside f
ls()                            # lists f
ls(f)                           # lists foo
attach(f)                       # attach f to the current environment
foo                             # foo can now be accessed directly
## bar
ls()                            # but still only shows f
rm(f)                           # can even remove f
foo                             # and foo is still accessible
## bar

Of course, there are some risks to using attach.

I redid the arcpyr package to use environments instead, but you can get the old behavior back by doing

arcpy = arcpy_env()
attach(arcpy)