0

I've been searching around the web but couldn't find a quick answer. Where do I put the libraries that my package will need so they get loaded also upon loading my package?

As I understand it, the imports parameter in the DESCRIPTION file will only install those packages if they do not currently exist on the system. But what is the solution to writing library(ggplot2) in a package, or am I supposed to write that for every script seperately?

When I in a new R session write devtools::load_all() and it loads my package, none of the libraries my package in turn depend upon is loaded.

uncool
  • 2,613
  • 7
  • 26
  • 55
  • Yes I know. That's what startled me. – uncool Jul 31 '15 at 21:32
  • 2
    Have you read through [Writing R Extensions](https://cran.r-project.org/doc/manuals/r-release/R-exts.html)? It goes into great detail about what belongs in which field. See section 1.1.1 – Rich Scriven Jul 31 '15 at 21:37

2 Answers2

6

If you haven't already done so, I'd strongly recommend reading Hadley Wickham's R packages guide. For your current question, the relevant bit is here.

With current versions of R, the preferred thing for the most part is to put required packages in Imports in DESCRIPTION and then to use the fully qualified function names using the :: function (e.g. plyr::llply). For functions that you use quite a bit, you can import them using import and importFrom in NAMESPACE. This is particularly relevant for things like infix functions (e.g. %>% from magrittr). roxygen2 makes this much easier by allowing the imports to be specified in the documentation block adjacent to the function.

The main reason for putting something into Depends rather than Imports in DESCRIPTION is where the user would expect the functions to be in their namespace because they need to use them directly.

Many packages on CRAN and Bioconductor still have everything in Depends, but it makes for a much more crowded namespace for the user and makes collisions between function names more likely.

Nick Kennedy
  • 12,510
  • 2
  • 30
  • 52
1

If you write package names to the Depends: field in the description, it will load them as well when you load the package. As far as I know, imports does the same but doesn't place the loaded library to the global namespace. For more info see: Better explanation of when to use Imports/Depends

So you just don't have to use library() inside a package to call other packages

Community
  • 1
  • 1
OganM
  • 2,543
  • 16
  • 33