【问题标题】:R package with Rcpp带有 Rcpp 的 R 包
【发布时间】:2021-04-24 18:13:58
【问题描述】:

我尝试使用 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
}

【问题讨论】:

    标签: c++ r package rcpp


    【解决方案1】:

    感谢您发布回购链接。

    当我使用我的(当前)版本的Rcpp 和对compileAttributes() 的调用重新创建RcppExports.{cpp,R} 时,它对我有用。你有什么版本的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:您的 repo 中有您的包的第二个副本。我只是顶级的。此外,您从您的 mac 提交了 .DS_Store 文件,这些文件对 R 或 repo 没有用。

    【讨论】:

    • 感谢您的回答。我使用的是 Rcpp 1.0.4。我现在已经更新到 Rcpp 1.0.6。我可能只是很迷茫。我试图删除 src/RcppExports.cpp 和 R/RcppExports.r 文件并运行 compileAttributes("/path/to/my/package"),它重新创建了文件。然后我再次推送git push 并尝试使用install_github 重新安装软件包,但我仍然收到错误消息。我究竟做错了什么?请注意,我没有使用R CMD 命令,我也不是littler。我真的不知道他们是什么......
    • 您可以从接受答案开始。然后通过非常缓慢地复制说明。 I.e. compileAttributes(".") 在您所在的目录中。然后R CMD build .R CMD INSTALL yourPackageHere_1.2.3.tar.gz。这一切都被记录在案,并且似乎适用于成千上万的人。将重点放在基本命令上并正确处理。这里gitdevtools 等等都会让我分心,但是不同的人有不同的工作流程。有些人只是更喜欢在 RStudio 中点击“重建包”按钮 ....
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多