Christoph
Christoph

Reputation: 7063

Minimal reproducible example package using Rcpp does not work

In short: I tried with SO and chatGPT, at some point I thought the NAMESPACE file was the error and deleted it. However, I still get an error when I run

Rcpp::compileAttributes(pkgdir = "path2package/testRcpp/", verbose = TRUE)

Error:

Error in Rcpp::compileAttributes(pkgdir = "path2package/testRcpp/",  :
pkgdir must refer to the directory containing an R package

I also created a new identical package from the scratch and build it:

Rcpp::compileAttributes(verbose = TRUE)
Exports from path2package/testRcpp2/src/CppFunctions.cpp:
   double meanC(NumericVector x)

keine nativen Symbole extrahiert
path2package/testRcpp2/src/RcppExports.cpp updated.
path2package/testRcpp2/R/RcppExports.R updated.

and used

devtools::document()
ℹ Updating testRcpp2 documentation
First time using roxygen2. Upgrading automatically...
Setting `RoxygenNote` to "7.2.3"
ℹ Loading testRcpp2
Exports from path2package/testRcpp2/src/CppFunctions.cpp:
   double meanC(NumericVector x)

keine nativen Symbole extrahiert
path2package/testRcpp2/src/RcppExports.cpp updated.
path2package/testRcpp2/R/RcppExports.R updated.
ℹ Re-compiling testRcpp2 (debug build)
── R CMD INSTALL ────────────────────────────────────────────────────────────────────────────────────
─  installing *source* package 'testRcpp2' ... (356ms)
   ** using staged installation
   ** libs
   g++ -std=gnu++11  -I"C:/R/R-4.2.2/include" -DNDEBUG  -I'C:/R/R-4.2.2/library/Rcpp/include'   -I"C:/rtools42/x86_64-w64-mingw32.static.posix/include"     -O2 -Wall -gdwarf-2 -mfpmath=sse -msse2 -mstackrealign  -UNDEBUG -Wall -pedantic -g -O0 -fdiagnostics-color=always -c CppFunctions.cpp -o CppFunctions.o
   g++ -std=gnu++11  -I"C:/R/R-4.2.2/include" -DNDEBUG  -I'C:/R/R-4.2.2/library/Rcpp/include'   -I"C:/rtools42/x86_64-w64-mingw32.static.posix/include"     -O2 -Wall -gdwarf-2 -mfpmath=sse -msse2 -mstackrealign  -UNDEBUG -Wall -pedantic -g -O0 -fdiagnostics-color=always -c RcppExports.cpp -o RcppExports.o
   g++ -std=gnu++11 -shared -static-libgcc -o testRcpp2.dll tmp.def CppFunctions.o RcppExports.o -LC:/rtools42/x86_64-w64-mingw32.static.posix/lib/x64 -LC:/rtools42/x86_64-w64-mingw32.static.posix/lib -LC:/R/R-4.2.2/bin/x64 -lR
   installing to C:/Users/CHRIST~1.STR/AppData/Local/Temp/Rtmpojw9xx/devtools_install_45bc13bd1b7f/00LOCK-testRcpp2/00new/testRcpp2/libs/x64
─  DONE (testRcpp2)
Writing meanC.Rd
Warning messages:
1: Skipping NAMESPACE
✖ It already exists and was not generated by roxygen2. 
2: Skipping NAMESPACE
✖ It already exists and was not generated by roxygen2. 

Restarting R session...

So eveything looks fine. But

testRcpp2::meanC(c(1, 2, 3))
Error in .Call("_testRcpp_meanC", PACKAGE = "testRcpp", x) : 
  "_testRcpp_meanC" not available for .Call() for package "testRcpp"

After deleting the NAMESPACE everything is screwed up again :-(

Any help is highly appreciated!


Details: I have a package in "path2package/testRcpp" with the following structure

testRcpp/
├── DESCRIPTION
├── R/
│   └── functions.R
├── src/
│   └── CppFunctions.cpp
└── man/

with

dir.exists("path2package/testRcpp/")
# [1] TRUE
file.exists("path2package/testRcpp/DESCRIPTION")
# [1] TRUE

and

list.files(all.files = TRUE)
 [1] "."  ".." ".git" ".gitignore" ".Rbuildignore" 
 [6] ".Rhistory" ".Rproj.user" "DESCRIPTION" "man" "R" 
[11] "src" "testRcpp.Rproj" "tests"       

I have a file src/CppFunctions.cpp with content

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
double meanC(NumericVector x) {
  int n = x.size();
  double total = 0;

  for(int i = 0; i < n; ++i) {
    total += x[i];
  }
  return total / n;
}

and a file R/functions.R with content

#' @useDynLib mypackage
#' @importFrom Rcpp sourceCpp
NULL

# Example function documentation

#' Calculate the mean using a C++ function
#'
#' @param x A numeric vector
#' @return A numeric, the mean of the vector
#' @export
meanC <- function(x) {
  .Call('_testRcpp_meanC', PACKAGE = 'testRcpp', x)
}

The DESCRIPTION file contains

Package: testRcpp
Type: Package
Title: What the Package Does (Title Case)
Version: 0.1.0
Author: Who wrote it
Maintainer: The package maintainer <[email protected]>
Description: More about what it does (maybe more than one line)
    Use four spaces when indenting paragraphs within the Description.
License: What license is it under?
Encoding: UTF-8
Depends: R (>= 3.5.0)
Imports: Rcpp (>= 1.0.5)
LinkingTo: Rcpp
LazyData: true
Suggests: 
    testthat (>= 3.0.0)
Config/testthat/edition: 3
RoxygenNote: 7.2.3

I deleted the NAMESPACE file, because I thought this could be the error... Now, if I run

Rcpp::compileAttributes(pkgdir = "path2package/testRcpp/", verbose = TRUE)

I get the error

Error in Rcpp::compileAttributes(pkgdir = "path2package/testRcpp/", : pkgdir must refer to the directory containing an R package

Upvotes: 2

Views: 65

Answers (0)

Related Questions