Rpackage: Rstudio probably creates a bad NAMESPACE file
I am making an R package in Rstudio and I have selected the option Configure Build Tools > Configure
and select Use roxygen to generate NAMESPACE
. I wrote my functions in Rcpp and it looks like NAMESPACE
when I generated it with roxygen2
:
# Generated by roxygen2 (4.1.1): do not edit by hand
export(function1)
export(function2)
export(function3)
export(function4)
Since my functions are written with Rcpp, which I then export, then they will be used in R via .Call
. However, from writing R extensions we should use useDynLib()
in such a case. This is why I think I am getting an error when I try to invoke function1
, and the error is:
Error in .Call("Mypackage_function1", PACKAGE = "Mypackage", var1, :
"Mypackage_function1" not available for .Call() for package "Mypackage"
When I use the default NAMESPACE
, when I run the project in Rstudio, I have the following in NAMESPACE
:
useDynLib(packagename)
exportPattern("^[[:alpha:]]+")
importFrom(Rcpp, evalCpp)
When I use the default NAMESPACE
, I can call functions using .Call
, however, I get a warning when I check a package that I am not generating NAMESPACE
using roxygen.
Is there a fix for this? Any advice is appreciated.
source to share
This is unrelated to the use of RStudio: for Roxygen to generate the appropriate spec, useDynLib
you need to use the tag @useDynLib
in Roxygen Doc comment :
#' @useDynLib packagename
You can do this anywhere (where you can use normal Roxygen comments), but it makes sense to put this in the package's documentation rather than the specific function's documentation. This is usually found in a file named R/packagename-package.r
:
#' My package documentation
#' … bla bla …
#' @name packagename
#' @docType package
#' @useDynLib packagename
NULL
source to share