2
votes

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

2
Here is a great resource for package development: r-pkgs.orgAdam

2 Answers

1
votes

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.

1
votes

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.