3

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.

ccshao
  • 499
  • 2
  • 8
  • 19
  • 2
    Have you tried just writing it once? Was there a problem? Seems like something that's pretty easy to test. If you have problems, try to give a specific error message or a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) so we can see what's really going on in order to give a more precise answer. – MrFlick Jul 27 '18 at 14:57
  • 1
    If you are trying to import a function for use in your package then you only need to use `@import [function to import]` once. Then it will be available throughout your package as long it is also in your DESCRIPTION file as an import. Often I think it is just easier to reference the package directly using `::` – boshek Jul 27 '18 at 15:35
  • 1
    It is sufficient to import once, but you can import several times without issue (that's useful if you want to copy-paste in another package for example). – Stéphane Laurent Jul 27 '18 at 15:38

1 Answers1

4

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

divibisan
  • 11,659
  • 11
  • 40
  • 58