3

I managed to build and install my R package and upon loading, all the functions in it are exported to the global environment and are visible from ls(). How can I prevent this so they behave like packages on CRAN and not pollute the global environment?

wdkrnls
  • 4,548
  • 7
  • 36
  • 64
  • 2
    How did you manage to export all your functions in the global environment? – nicola Apr 01 '15 at 13:54
  • Visible from `ls()` or from `ls(pos=2)` or whichever `pos` the package is loaded at? – Spacedman Apr 01 '15 at 13:59
  • `ls()`, with the default pos argument. – wdkrnls Apr 01 '15 at 14:06
  • did you source the r script with your functions before building your package? does this happen when you load the package in a fresh r session? – rawr Apr 01 '15 at 14:16
  • I grep'd for any `source` functions in the package and couldn't find any. This happens on a fresh R session. – wdkrnls Apr 01 '15 at 14:33
  • 3
    Presumably you cleared the functions from the global environment before reloading the fresh session? R will save the workspace and reload it, so if you're not actively clearing the functions starting in a fresh session won't necessarily remove them. – BrodieG Apr 01 '15 at 15:03
  • You were right, there was an .Rdata file and it was loading all the functions from the start. Deleting it, the functions in the package are kept separate. – wdkrnls Apr 01 '15 at 15:11

1 Answers1

-1

You can have them start with a leading dot ., if you simply do not want them to be listed when ls() is used with default parameters. However, that does not change their namespace or environment. You can still see them using ls(all.names=TRUE).

Christopher Bottoms
  • 11,218
  • 8
  • 50
  • 99
GWD
  • 1,387
  • 10
  • 22
  • have a look at point 3 of http://stackoverflow.com/questions/7526467/what-does-the-dot-mean-in-r-personal-preference-naming-convention-or-more took me some time to look up and find it - but has already been answered in SO ... – GWD Apr 01 '15 at 13:57
  • I didn't downvote this, but most likely [they simply should not](http://cran.r-project.org/doc/manuals/r-release/R-exts.html#Specifying-imports-and-exports) export "all the functions". – Roland Apr 01 '15 at 14:04
  • 2
    @Roland I think even if you export all the functions, they don't show up in the global environment. – Avinash Apr 01 '15 at 14:58
  • 1
    Surely the OP doesn't want a package where every function begins with a dot. Hiding a function from `ls()` when called with its defaults does not solve the problem of having all the objects in the `globalenv()` – GSee Apr 01 '15 at 15:53
  • First - the functions supposed to be visible most probably won't start with a dot (.) Second - the question still was: "How do I prevent my packaged functions from being listed with ls()". Third - if he already has his package build I am not sure if rewriting the whole thing via extensive use of "environment()" would be easier to achieve. – GWD Apr 01 '15 at 19:15
  • 1
    The goal is to make the package "behave like packages on CRAN and not pollute the global environment". Hiding them is still pollution as could be seen with `ls(all.names=TRUE)`. The functions need to be in the package's namespace, not the global environment. Just like any function you're used to (`ls`, `cat`, `lm`, etc.). When you type `ls()` you don't see those functions (and they don't begin with a dot), but they are still accessible because they are in an environment (other than .GlobalEnv) that is attached to the search path. – GSee Apr 02 '15 at 12:14