简体   繁体   中英

R: How do I make my package use another package?

This is very simple question. I am extending someone's package. It currently uses packages A, B and they are listed in the DESCRIPTION file.

If I need functions from package C - to add a package to the dependencies - do I just add the package in the DESCRIPTION file and that is all that is needed? Into what section - Depends or Imports? Are there more other steps to make? Do I need to use prefix C::functionInC() once my code needs to use a package C function?

Short answer: Add C to Imports: and when using the C functions, use the double semicolon prefix.

Longer context:

The link below provides the following advice http://r-pkgs.had.co.nz/namespace.html#imports

R functions

If you are using just a few functions from another package, my recommendation is to note the package name in the Imports: field of the DESCRIPTION file and call the function(s) explicitly using ::, eg, pkg::fun().

If you are using functions repeatedly, you can avoid :: by importing the function with @importFrom pgk fun. This also has a small performance benefit, because :: adds approximately 5 µs to function evaluation time.

Alternatively, if you are repeatedly using many functions from another package, you can import all of them using @import package. This is the least recommended solution because it makes your code harder to read (you can't tell where a function is coming from), and if you @import many packages, it increases the chance of conflicting function names.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM