7

My R package uses an internal variable x. If I load the package (I've only tried using devtools::load_all), then x doesn't appear in the ls() list, but it does have a value. How can I avoid this?

I'm fine with the user being able to access the variable with myPackage::x, but not simply x.

GSee
  • 48,880
  • 13
  • 125
  • 145
Andreas
  • 7,470
  • 10
  • 51
  • 73

2 Answers2

9

The load_all function has an export_all argument.

From ?load_all

If TRUE (the default), export all objects. If FALSE, export only the objects that are listed as exports in the NAMESPACE file.

So, try using export_all=FALSE in your load_all call.

GSee
  • 48,880
  • 13
  • 125
  • 145
6

Try building the package first, and check whether the problem still exists. The exports from a package are defined in the NAMESPACE file. When you use devtools::load_all, the namespace isn't loaded ( see here). Read more about this and building a package in the manual Writing R extensions.

You might be using a default export pattern in your NAMESPACE file. Check it in your package, and if it looks like this:

exportPattern("^[^\\.]")

then the package exports everything from the namespace that doesn't start with a dot. So you either call it .x, or you change the exportPattern() to eg...

export(myfun1, myfun2) 

to export the functions myfun1 and myfun2 from the package. By explicitly defining what you want to export, you avoid that something's available when there's no need for that.

Joris Meys
  • 106,551
  • 31
  • 221
  • 263
  • My namespace file is just `export(f,g)` – Andreas Nov 18 '12 at 00:31
  • 1
    Try building the package first, and make sure you specify the export in the namespace correctly. See also my edit. – Joris Meys Nov 18 '12 at 00:38
  • I see. Is there any way to avoid building and installing the package every time I make an edit? I test it with `source("test.R")`, and building-installing before that is a bit clunky. – Andreas Nov 18 '12 at 00:40
  • you don't have to build the package for every test, but if you want to check whether everything is exported as you wish, you have to install it. Otherwise the namespace file isn't read. But this is something I do completely at the end, when I'm sure all the rest works as intended. – Joris Meys Nov 18 '12 at 00:42
  • 1
    Andreas you keep asking about building a package and kind of blew of suggestions to use Rstudio before. I promise it would simplify your life a lot to use it. There are package development tools to do a lot of what you're asking. Also may I suggest going to git hub and find someone's package there you trust (I trust Hadley Wickham's for example) and look at how they've done things. – Tyler Rinker Nov 18 '12 at 01:26
  • @TylerRinker Thanks for caring. I can be a bit stubborn about using the "most fundamental" tools sometimes. I'll check out Rstudio. – Andreas Nov 18 '12 at 11:27