带有 Rcpp 的 R 包

R package with Rcpp

我尝试使用 Rcpp 将一个小的 C++ 函数(称为 reduceString)添加到我的 R 包中,但我未能配置该包以使其编译正常。可以找到包 here.

devtools::install_github("RemiMattheyDoeret/SimBitWrapper")

这是错误信息的一部分

Error: package or namespace load failed for ‘SimBitWrapper’ in dyn.load(file, DLLpath = DLLpath, ...):
 unable to load shared object '/Library/Frameworks/R.framework/Versions/3.5/Resources/library/SimBitWrapper/libs/SimBitWrapper.so':
  dlopen(/Library/Frameworks/R.framework/Versions/3.5/Resources/library/SimBitWrapper/libs/SimBitWrapper.so, 6): Symbol not found: __Z12reduceStringv
  Referenced from: /Library/Frameworks/R.framework/Versions/3.5/Resources/library/SimBitWrapper/libs/SimBitWrapper.so
  Expected in: flat namespace
 in /Library/Frameworks/R.framework/Versions/3.5/Resources/library/SimBitWrapper/libs/SimBitWrapper.so

我试过一次又一次地修改NAMESPACEDESCRIPTION文件以及src/RcppExports.cppR/RcppExports.r文件,但我还是没能解决问题。这是那些文件以及 reduceString.cpp 文件

命名空间

useDynLib(SimBitWrapper, .registration=TRUE)
exportPattern("^[^\.]")
importFrom(Rcpp, evalCpp)

描述

Package: SimBitWrapper
Title: R wrapper for SimBit
Version: 5.1.0
Authors@R: 
    person(given = "Remi",
           family = "Matthey-Doret",
           role = c("aut", "cre"),
           email = "remi.b.md@gmail.com",
           comment = c(ORCID = "0000-0001-5614-5629"))
Description: The package is a wrapper for SimBit (SimBit is a simulation platform for evolutionary genetic studies). Please see the SimBit manual (at https://github.com/RemiMattheyDoret/SimBit) for description of how to use this SimBitWrapper package.
License: MIT
Encoding: UTF-8
LazyData: true
Depends: R (>= 2.15)
Imports: data.table, processx, Rcpp
LinkingTo: Rcpp
Suggests:
URL: https://github.com/RemiMattheyDoret

src/RcppExports.cpp

#include <Rcpp.h>

using namespace Rcpp;

// reduceString
std::string reduceString();
RcppExport SEXP _SimBitWrapper_reduceString() {
BEGIN_RCPP
    Rcpp::RObject rcpp_result_gen;
    Rcpp::RNGScope rcpp_rngScope_gen;
    rcpp_result_gen = Rcpp::wrap(reduceString());
    return rcpp_result_gen;
END_RCPP
}

static const R_CallMethodDef CallEntries[] = {
    {"_SimBitWrapper_reduceString", (DL_FUNC) &_SimBitWrapper_reduceString, 0},
    {NULL, NULL, 0}
};

RcppExport void R_init_SimBitWrapper(DllInfo *dll) {
    R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
    R_useDynamicSymbols(dll, FALSE);
}

R/RcppExports.r

reduceString <- function(x) {
    .Call(`_SimBitWrapper_reduceString`,x)
}

reduceString.cpp

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
std::string reduceString(CharacterVector& x)
{
    // I cut off this part that does not need to be printed here
}

感谢您将 link 发布到 repo。

它对我有用当我使用我的(当前)版本的 Rcpp 重新创建 RcppExports.{cpp,R} 并调用 compileAttributes().你有什么版本的Rcpp

下面的日志使用了我在 littler 中的包装器,但这并不重要。 R CMD ... 命令的工作方式相同。

日志

edd@rob:/tmp/SimBitWrapper(master)$ build.r 
* checking for file ‘./DESCRIPTION’ ... OK
* preparing ‘SimBitWrapper’:
* checking DESCRIPTION meta-information ... OK
* cleaning src
* checking for LF line-endings in source and make files and shell scripts
* checking for empty or unneeded directories
* building ‘SimBitWrapper_5.1.0.tar.gz’

edd@rob:/tmp/SimBitWrapper(master)$ install2.r -l /tmp/lib SimBitWrapper_5.1.0.tar.gz 
* installing *source* package ‘SimBitWrapper’ ...
** using staged installation
** libs
ccache g++ -I"/usr/share/R/include" -DNDEBUG  -I'/usr/local/lib/R/site-library/Rcpp/include'    -fpic  -g -O3 -Wall -pipe -pedantic  -c RcppExports.cpp -o RcppExports.o
ccache g++ -I"/usr/share/R/include" -DNDEBUG  -I'/usr/local/lib/R/site-library/Rcpp/include'    -fpic  -g -O3 -Wall -pipe -pedantic  -c reduceString.cpp -o reduceString.o
reduceString.cpp: In function ‘std::string reduceString(Rcpp::CharacterVector&)’:
reduceString.cpp:10:27: warning: comparison of integer expressions of different signedness: ‘size_t’ {aka ‘long unsigned int’} and ‘R_xlen_t’ {aka ‘long int’} [-Wsign-compare]
   10 |     for (size_t i = 0 ; i < x.size() ; ++i)
      |                         ~~^~~~~~~~~~
ccache g++ -Wl,-S -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o SimBitWrapper.so RcppExports.o reduceString.o -L/usr/lib/R/lib -lR
installing to /tmp/lib/00LOCK-SimBitWrapper/00new/SimBitWrapper/libs
** R
** byte-compile and prepare package for lazy loading
** help
No man pages found in package  ‘SimBitWrapper’ 
*** 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 (SimBitWrapper)
edd@rob:/tmp/SimBitWrapper(master)$ 

差异

edd@rob:/tmp/SimBitWrapper(master)$ git diff
diff --git a/R/RcppExports.R b/R/RcppExports.R
index 19a82b4..62f890a 100644
--- a/R/RcppExports.R
+++ b/R/RcppExports.R
@@ -2,6 +2,6 @@
 # Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
 
 reduceString <- function(x) {
-    .Call(`_SimBitWrapper_reduceString`,x)
+    .Call(`_SimBitWrapper_reduceString`, x)
 }
 
diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp
index 90dda3b..0b91977 100644
--- a/src/RcppExports.cpp
+++ b/src/RcppExports.cpp
@@ -6,18 +6,19 @@
 using namespace Rcpp;
 
 // reduceString
-std::string reduceString();
-RcppExport SEXP _SimBitWrapper_reduceString() {
+std::string reduceString(CharacterVector& x);
+RcppExport SEXP _SimBitWrapper_reduceString(SEXP xSEXP) {
 BEGIN_RCPP
     Rcpp::RObject rcpp_result_gen;
     Rcpp::RNGScope rcpp_rngScope_gen;
-    rcpp_result_gen = Rcpp::wrap(reduceString());
+    Rcpp::traits::input_parameter< CharacterVector& >::type x(xSEXP);
+    rcpp_result_gen = Rcpp::wrap(reduceString(x));
     return rcpp_result_gen;
 END_RCPP
 }
 
 static const R_CallMethodDef CallEntries[] = {
-    {"_SimBitWrapper_reduceString", (DL_FUNC) &_SimBitWrapper_reduceString, 0},
+    {"_SimBitWrapper_reduceString", (DL_FUNC) &_SimBitWrapper_reduceString, 1},
     {NULL, NULL, 0}
 };
 
edd@rob:/tmp/SimBitWrapper(master)$ 

PS:你的仓库中有你的包的第二个副本。我只是顶级。此外,您提交了 mac 中的 .DS_Store 文件,这些文件对 R 或 repo 没有用。