I have an R package that requires functions from several other packages to be in the namespace. Using roxygen2 documentation, I've successfully installed these packages (i.e. install.packages(dplyr)
), but I am unable to load them in automatically (i.e. library(dplyr)
).
Here is my DESCRIPTION file:
Package: pkgname
Title: What the Package Does (one line, title case)
Version: 0.0.0.9000
Authors@R: person("First", "Last", email = "[email protected]", role = c("aut", "cre"))
Description: What the package does (one paragraph).
Depends: R (>= 3.5.2)
Imports:
ggplot2,
zoo,
tidyr,
dplyr,
magrittr
Suggests:
RColorBrewer
License: What license is it under?
Encoding: UTF-8
LazyData: true
RoxygenNote: 6.1.1
At the end of the documentation for one of my functions that requires the pipe function from magrittr, I included the following notation:
#' @importFrom magrittr %>%
#'
#' @export
funName <- function(...) {
...
}
And the other function:
#' @import ggplot2
#' @importFrom magrittr %>%
#'
#' @export
funName2 <- function(...) {
...
}
And this successfully shows up in my NAMESPACE file:
# Generated by roxygen2: do not edit by hand
export(funName2)
export(funName)
import(ggplot2)
importFrom(magrittr,"%>%")
However, despite the added notation in roxygen2 comments and the correct script in my NAMESPACE file, I still have to load the packages ggplot2
and magrittr
using library(package-name)
at every new R session. I expect this requirement for dplyr
, tidyr
and zoo
(since I do not explicitly load these, just install them), but I did not for ggplot2
or the %>%
operator. Am I importing them incorrectly?
You should never use library(package.name)
inside your package functions. Instead use package.name::function.name()
.
You need to re-export the magrittr
pipe operator:
1- put magrittr
into the DESCRIPTION file (as you did)
2- make an __imports.R
file to the packages R/
directory with the following lines:
#' re-export magrittr pipe operator
#'
#' @importFrom magrittr %>%
#' @name %>%
#' @rdname pipe
#' @export
NULL
Or, similarly do as Hadley Wickham says:
#' Pipe operator
#'
#' @name %>%
#' @rdname pipe
#' @keywords internal
#' @export
#' @importFrom magrittr %>%
#' @usage lhs \%>\% rhs
NULL
I think you can do the same for other imports when necessary. Use @importFrom
as much as you can, otherwise re-export it.