简体   繁体   English

R package 带 Rcpp

[英]R package with Rcpp

I tried to add a small C++ function (called reduceString ) into an R package of mine using Rcpp but I failed to configurate the package so that it compiles fine. I tried to add a small C++ function (called reduceString ) into an R package of mine using Rcpp but I failed to configurate the package so that it compiles fine. The package can be found here . package 可以在这里找到。

devtools::install_github("RemiMattheyDoeret/SimBitWrapper")

Here is part of the error message这是错误消息的一部分

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

I have tried to modify again and again the NAMESPACE and DESCRIPTION files as well as src/RcppExports.cpp and R/RcppExports.r files but I just fail to fix the problem.我试图一次又一次地修改NAMESPACEDESCRIPTION文件以及src/RcppExports.cppR/RcppExports.r文件,但我只是无法解决问题。 Here are those files as well as the reduceString.cpp file以下是这些文件以及reduceString.cpp文件

NAMESPACE命名空间

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

DESCRIPTION描述

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 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 R/RcppExports.r

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

reduceString.cpp减少字符串.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
}

Thanks for posting a link to a repo.感谢您发布指向回购的链接。

It worked for me as soon as I re-recreated RcppExports.{cpp,R} using my (current) version of Rcpp and a call to compileAttributes() .只要我使用我的(当前)版本的Rcpp和对compileAttributes()的调用重新创建RcppExports.{cpp,R} ,它就对我有用。 What version of Rcpp do you have?你有什么版本的Rcpp

The log below uses my wrappers from littler but that is immaterial.下面的日志使用了littler的包装器,但这无关紧要。 The R CMD... commands would have worked the same way. R CMD...命令的工作方式相同。

Log日志

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)$ 

Diff差异

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: You have a second copy of your package in your repo. PS:您的仓库中有第二份 package。 I just the top-level.我只是顶级的。 Also, you committed the .DS_Store files from your mac which are of no use to R or a repo.此外,您从 Mac 提交了对 R 或 repo 没有用的.DS_Store文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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