19

I have read two recent posts that discuss Depends and Imports

But I have four lingering, related questions:

  1. Suppose I want two packages to also be available to the end-user when they load in my package. Is there a good reason not to use Depends in this context? (The point here is to load all three packages via a command that loads just the one package.)

  2. Is it okay to specify a package in both the Depends and Imports fields?

  3. If a package is listed in Depends, is there a point to also listing it in Imports? Or are the benefits of Imports already negated by using Depends?

  4. Is the following correct? A package should be listed in the Imports field of the DESCRIPTION file if and only if the package is imported (in whole or in part) in the NAMESPACE file.

Thanks much!

David

Community
  • 1
  • 1
David Diez
  • 679
  • 1
  • 6
  • 13
  • 1
    Very much related: http://stackoverflow.com/q/8637993/602276 – Andrie Mar 27 '12 at 19:44
  • 1
    Now I see - you did in fact link to the 2 previous posts, but that wasn't very obvious. I edited your question to make that much more explicit. Nice question, by the way. +1 – Andrie Mar 27 '12 at 19:56

1 Answers1

11

Couple of points, and I will admit that I also find this confusing at times. But I revisited it recently, and here is my take:

  1. "Depends" is how we used to do things; it is closest to "just loading all three":when your third depends on the other two, all three will get loaded.

  2. With Namespaces, we can also import. That brings in just the stated symbols, which can be data or functions. I use this sometimes; it will not load the other package that you import from but just make the stated symbols available. As such, it is "lighter" than Depends.

  3. If you do Depends, there is no need for Imports.

  4. That is correct: If you use declarations in in NAMESPACE to import symbols from another packages, that other package needs to be listed in Imports: in the DESCRIPTION file.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • 1
    Nice answer.In addition to this, my take is that `Imports` is safer than `Depends`. I am very likely using the wrong terminology here, but if you `Import`, then the imported packages lives in a "stack" that is "internal" to the namespace of the importing package. This protects against the scenario where other loaded packages subsequently masks functions. If this vague description is remotely accurate, can you please add a proper description in your answer? – Andrie Mar 27 '12 at 19:40
  • I think I know what you mean, but can't word it in a way I'm happy with. But your main point is good: "Import" is finer-grained and less likely to have side-effects. – Dirk Eddelbuettel Mar 27 '12 at 19:41
  • Ahah, I knew there was a similar question and answer recently. @josho'brien to the rescue in http://stackoverflow.com/a/8638902/602276 – Andrie Mar 27 '12 at 19:43
  • Thanks for your replies! Moving forward, I'll use Imports whenever possible (always coordinating with the NAMESPACE file), and Depends as a sledgehammer of sorts only when I really want the end-user to be able to access the other packages features directly. Also, thanks Andrie for making the links above in the original much clearer. – David Diez Mar 27 '12 at 20:24