简体   繁体   中英

Unable to find compiled C++ functions in R package

I'm having difficulty integrating C++ functions into my R package. I'm following Hadley's guide step by step, and I'm curious if perhaps it needs an update. I created a tiny test package for reproducibility.

To create this package, I follow these steps:

(1) Create the call_times_two function in hello.R .

(2) Call devtools::document() .

(3) Call usethis::use_rcpp() .

(4) Add the suggested roxygen tags to R/rcpptest-package.R .

(5) Add Hadley's timesTwo function to src/code.cpp .

(6) Call devtools::document() again.

(7) Build and reload the package.

No errors were encountered in any of the above steps.

Then, I attempt to call my call_times_two function as

# Expected output of 6
rcpptest::call_times_two(3)

However, when I call this function I get the following error:

Error in .Call("_rcpptest_timesTwo", PACKAGE = "rcpptest", x) : 
  "_rcpptest_timesTwo" not available for .Call() for package "rcpptest"

Does anyone know why this is happening?

Maybe that guide or version is outdated? We have supported Rcpp.packages.skeleton() pretty much since Rcpp was first released.

Create

$ Rscript -e 'Rcpp::Rcpp.package.skeleton("rcpptest")'
Creating directories ...
Creating DESCRIPTION ...
Creating NAMESPACE ...
Creating Read-and-delete-me ...
Saving functions and data ...
Making help files ...
Done.
Further steps are described in './rcpptest/Read-and-delete-me'.

Adding Rcpp settings
 >> added Imports: Rcpp
 >> added LinkingTo: Rcpp
 >> added useDynLib directive to NAMESPACE
 >> added importFrom(Rcpp, evalCpp) directive to NAMESPACE
 >> added example src file using Rcpp attributes
 >> added Rd file for rcpp_hello_world
 >> compiled Rcpp attributes 
$ 

Install

$ R CMD INSTALL rcpptest
* installing to library ‘/usr/local/lib/R/site-library’
* installing *source* package ‘rcpptest’ ...
** using staged installation
** libs
ccache g++-11 -I"/usr/share/R/include" -DNDEBUG  -I'/usr/local/lib/R/site-library/Rcpp/include'    -fpic  -g -O3 -Wall -pipe   -c RcppExports.cpp -o RcppExports.o
ccache g++-11 -I"/usr/share/R/include" -DNDEBUG  -I'/usr/local/lib/R/site-library/Rcpp/include'    -fpic  -g -O3 -Wall -pipe   -c rcpp_hello_world.cpp -o rcpp_hello_world.o
ccache g++-11 -Wl,-S -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -flto=auto -Wl,-z,relro -o rcpptest.so RcppExports.o rcpp_hello_world.o -L/usr/lib/R/lib -lR
installing to /usr/local/lib/R/site-library/00LOCK-rcpptest/00new/rcpptest/libs
** R
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
** checking absolute paths in shared objects and dynamic libraries
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (rcpptest)
$ 

Use

> library(rcpptest)
> rcpp_hello_world()
[[1]]
[1] "foo" "bar"

[[2]]
[1] 0 1

> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM