Home > Software engineering >  How to edit NAMESPACE with roxygen2 when creating R packages?
How to edit NAMESPACE with roxygen2 when creating R packages?

Time:11-24

I am writing an R package and in the following link,

How can I load dependencies in an R package? a response indicates that it is necessary to "add a line in your NAMESPACE file importFrom(ncdf4, nc_open) and then in your code, call the function without the package: nc_open(...)"

and here, How to properly include dependencies in R-package? an user says:

"The NAMESPACE file. Here you declare the packages you need

import(ggplot2) or to avoid namespace clashes

importFrom(ggplot2, geom_point) You can get roxygen2 to maintain the NAMESPACE file using the @import and @importFrom tags."

These recommendations look straightforward. However, as I create NAMESPACE with the r package roxygen2, the file NAMESPACE cannot be edited manually.

Then, how to edit the file NAMESPACE?

Very much thank you in advance

CodePudding user response:

You're supposed to use

#' @export

or

#' @importFrom 

preferably in the files (and above) the code that make use of these imports. See Vignette on Roxygen2, NAMESPACE tags Then a call to devtools::document() will generate the appropriate NAMESPACE, etc. through these annotations.

CodePudding user response:

If you need full example, this is how it could look:

#' Title
#'
#' @return
#' @export
#' @import ggplot2
#' @importFrom data.table setDT
#' @examples
my_function <- function() {
  
}

To insert roxygen2 skeleton - go to "Code" tab in RStudio and search Insert Roxygen Skeleton.

tag @export indicates that my_function will be exported to be visible for users, when using package::my_fun or after library(package). Tag @import makes all exported functions from ggplot2 package available for you, i.e. no need to use ggplot2::aes() when you use functions from this package in your package. Tag @importFrom makes available only explicitly mentioned function from package, i.e. no need to use package::fun(), but it will be necessary for other functions from this package.

  • Related