Use dependencies in R packages via library () / description file

I am writing an R package that has several dependencies on other packages, some of them are available in CRAN, while others are self-made.

According to the help, it will library("my_package")load the package namespace as soon as I pre-installed it, i.e. install.package("my_package").

However, as soon as I installed the package, I can use all the functions of the installed but not downloaded package through my_package::my_function(), therefore, if my package has dependencies, in addition to adding them to the file DESCRIPTION:

Imports:
    dplyr,
    my_package2,
    ggvis,

at the root of the package folder. Do I need to download the dependencies of the new package through library(), or will the end user get an error if he is not installed on his computer, since the required packages are listed in the Import section?

+4
source share
1 answer

No, the user does not need to download packages that are used by features in my_package.

The fact that you specified a package under Imports:in the file DESCRIPTIONmeans that during the installation, my_packageR checks for the presence of this package on your system. This means that functions in my_packagecan use functions from these packages, using the notation ::, as you suggested.

:: - , :

  • , dplyr :: my_package, import(dplyr) NAMESPACE. , .

  • , , select dplyr, importFrom(select, dplyr) NAMESPACE.

  • DESCRIPTION Depends:. , , library(my_package). .

R , my_package " " , . , , , select() dplyr, . Depends:, . my_package , - select() , my_package , .

1:

DESCRIPTION :

Imports:
    dpylr

my_package:

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

2:

DESCRIPTION :

Imports:
    dpylr

NAMESPACE :

import(dplyr)

my_package:

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

3:

DESCRIPTION :

Imports:
    dpylr

NAMESPACE :

importFrom(select, dplyr)

my_package:

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

, R . , :

+5

Source: https://habr.com/ru/post/1624982/


All Articles