Search code examples
rroxygen2

import the same PACKAGE in several R files


When writing an R package, I need to import another R package B. I use the roxygen2 for the documentation.

My question is, if I have several R functions using the package B, should I write

#' @import B

for each function, or it is suffericent to only write one time.


Solution

  • As mentioned in the comments, you only need to import it once, but importing it many times doesn't cause any problems.

    If you don't want to import it in every function, but are worried about tying it to a single function (what if you only import it on function foo, but later you decided to replace foo with bar and lose the import) you can add all your shared import statements to NULL at the top of the document:

    #' @import ggplot2
    #' @import B
    #' @import dplyr
    NULL
    

    roxygen2 will happily create the proper import statements in NAMESPACE, but you'll only have the imports listed once in a convenient place without tying them to any particular package