6

I'm writing an R package that has several dependencies of other packages, some of them are available in CRAN and other ones are homemade.

According to the help, library("my_package") will load the namespace of the package once I have previously installed it, i.e, install.package("my_package").

Nevertheless, once I have installed the package I am able to use all the functions of the installed but not loaded package through my_package::my_function(), so if my package has dependencies, beside adding those into DESCRIPTION file:

Imports:
    dplyr,
    my_package2,
    ggvis,

in the root of the package folder. Do I have to load the dependencies of the new package through library() or the final user will see an error if he has not installed on his computer as the required packages are specified in the Imports section?

Cœur
  • 37,241
  • 25
  • 195
  • 267
blamblam
  • 423
  • 6
  • 20
  • 3
    You should never use `library` inside a package. The reason is, that your package code is executed once in the building process. So you should stick to the dependency fields in the DESCRIPTION file. – Martin Schmelzer Jan 19 '16 at 15:48
  • 2
    For further details just check the passage on dependencies in Hadley Wickham: http://r-pkgs.had.co.nz/description.html – Martin Schmelzer Jan 19 '16 at 15:50
  • 1
    @MartinDabbelJuSmelter A more important reason for not using `library` is, I feel, that it pollutes the `search()` of the package’s user. Libraries have no business changing the global state of the session (unfortunately R makes this occasionally hard, and many packages behave badly). – Konrad Rudolph Jan 19 '16 at 17:54

1 Answers1

7

No, the user does not have to load the packages that are used by functions in my_package.

The fact that you have listed a package under Imports: in the DESCRIPTION file means that during the installation of my_package, R will check that this package is available on your system. This means then that functions in my_package can use functions from these packages using the :: notation, as you suggested.

Using the :: notation is the recommended way to refer to functions from other packages, but there are also other options:

  • In order to make all the functions from, say, dplyr accessable without :: in my_package, you could add import(dplyr) to the NAMESPACE file. This is convenient, if you use many functions from a package.

  • If you intend to use only, say, the function select from dplyr, you could add importFrom(select, dplyr) to the NAMESPACE file.

  • You could also add the package to the DESCRIPTION file under Depends:. This would mean that the package is loaded to the global environment when you use library(my_package). This is almost never a good solution.

The general idea of dependencies is R is that my_package will have "it's own version" of the packages it depends on loaded. Therefore, you can always be sure that you will, e.g., use the function select() from the dplyr package, as you intended to do. The exception is the use of Depends: which bypasses this system. In this case, my_package will look for functions in the global environment and if somebody should have defined some function called select() in the global environment, my_package will use this function and you will get unexpected results.

Example 1:

DESCRIPTION file:

Imports:
    dpylr

some function from my_package:

my_fun <- function(...) {
    dplyr::mutate(...) %>%
    dplyr::select(1:3)
}

Example 2:

DESCRIPTION file:

Imports:
    dpylr

NAMESPACE file:

import(dplyr)

some function from my_package:

my_fun <- function(...) {
    mutate(...) %>%
    select(1:3)
}

Example 3:

DESCRIPTION file:

Imports:
    dpylr

NAMESPACE file:

importFrom(dplyr,select)

some function from my_package:

my_fun <- function(...) {
    dpylr::mutate(...) %>%
    select(1:3)
}

You find more detailed explanations of how to handle dependencies in R packages on the web. For instance the following are useful:

Also, it is not necessary to write the NAMESPACE file by hand. You can let roxygen2 do that for you. Read the documentation for more information.

Stibu
  • 15,166
  • 6
  • 57
  • 71
  • I would amend “almost never” to “never” when it comes to using `Depends` … but I know that some people disagree with this. Nevertheless, consider that you are polluting the user’s global state, and potentially changing the semantics of other loaded packages. – Konrad Rudolph Jan 19 '16 at 17:56
  • 1
    I fully agree with you that `Depends` should never be used. But I have experienced cases where a package that I import somehow need another package to be loaded to the global environment and I have not managed to get around using `Depends`. So it seems that bad design in other packages might force one to use `Depends` as well. This is what I meant: it should not be used, but sometimes there is no other practical way. – Stibu Jan 19 '16 at 18:00
  • 2
    The syntax is `importFrom(package, function)` and not `importFrom(select, dplyr)`. – s_baldur Jan 28 '20 at 16:15
  • @sindri_baldur You are right, of course. Thanks for the hint! – Stibu Jan 28 '20 at 17:45