c++ - 使用 Rcpp 创建模块时出错

标签 c++ r rcpp

我正在尝试使用 Rcpp 开发 R 模块。为此,我遵循了 ( Dirk Eddelbuettel's guide)

我图书馆的文件有以下内容:

函数.hpp:

class myclass {
    // my atributes and functions
}

函数.cpp:

#include <Rcpp.h>

using namespace Rcpp;

class myclass;

RCPP_EXPOSED_CLASS(myclass)

#include "functions.hpp"

//Implementation of my funcions

RCPP_MODULE(mymodule){
    class_<myclass>("myclass")
    .constructor()
    .method("oneMethod", &myclass::oneMethod)
    //more methods
;

mypackageExports.R:

.onLoad<-function(libname, pkgname){
    require("methods")
    loadRcppModules()
}

描述:

...
LazyLoad: yes
Depends: methods, Rcpp (>= 0.12.4)
LinkingTo: Rcpp
RcppModules: mymodule

命名空间:

useDynLib(mypackage)
exportPattern("^[[:alpha:]]+")
import(Rcpp)

当我使用命令 R CMD INSTALL mypackage 编译库时出现错误:

installing to /home/user/R/x86_64-pc-linux-gnu-library/3.3/mypackage/libs
** R
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded

*** caught segfault ***
address (nil), cause 'memory not mapped'

Traceback:
 1: .Call(Module__classes_info, xp)
 2: Module(m, pkg, mustStart = TRUE)
 3: doTryCatch(return(expr), name, parentenv, handler)
 4: tryCatchOne(expr, names, parentenv, handlers[[1L]])
 5: tryCatchList(expr, classes, parentenv, handlers)
 6: tryCatch({    mod <- Module(m, pkg, mustStart = TRUE)    if (isTRUE(direct)) {        populate(mod, ns)    }    else {        forceAssignInNamespace(m, mod, ns)    }    assign(.moduleMetaName(m), mod, envir = ns)}, error = function(e) {    stop(sprintf("failed to load module %s from package %s\n%s",         m, pkg, conditionMessage(e)))})
 7: loadRcppModules()
 8: fun(libname, pkgname)
 9: doTryCatch(return(expr), name, parentenv, handler)
10: tryCatchOne(expr, names, parentenv, handlers[[1L]])
11: tryCatchList(expr, classes, parentenv, handlers)
12: tryCatch(fun(libname, pkgname), error = identity)
13: runHook(".onLoad", env, package.lib, package)
14: loadNamespace(package, lib.loc)
15: doTryCatch(return(expr), name, parentenv, handler)
16: tryCatchOne(expr, names, parentenv, handlers[[1L]])
17: tryCatchList(expr, classes, parentenv, handlers)
18: tryCatch(expr, error = function(e) {    call <- conditionCall(e)    if (!is.null(call)) {        if (identical(call[[1L]], quote(doTryCatch)))             call <- sys.call(-4L)        dcall <- deparse(call)[1L]        prefix <- paste("Error in", dcall, ": ")        LONG <- 75L        msg <- conditionMessage(e)        sm <- strsplit(msg, "\n")[[1L]]        w <- 14L + nchar(dcall, type = "w") + nchar(sm[1L], type = "w")        if (is.na(w))             w <- 14L + nchar(dcall, type = "b") + nchar(sm[1L],                 type = "b")        if (w > LONG)             prefix <- paste0(prefix, "\n  ")    }    else prefix <- "Error : "    msg <- paste0(prefix, conditionMessage(e), "\n")    .Internal(seterrmessage(msg[1L]))    if (!silent && identical(getOption("show.error.messages"),         TRUE)) {        cat(msg, file = stderr())        .Internal(printDeferredWarnings())    }    invisible(structure(msg, class = "try-error", condition = e))})
19: try({    attr(package, "LibPath") <- which.lib.loc    ns <- loadNamespace(package, lib.loc)    env <- attachNamespace(ns, pos = pos, deps)})
20: library(pkg_name, lib.loc = lib, character.only = TRUE, logical.return = TRUE)
21: withCallingHandlers(expr, packageStartupMessage = function(c) invokeRestart("muffleMessage"))
22: suppressPackageStartupMessages(library(pkg_name, lib.loc = lib,     character.only = TRUE, logical.return = TRUE))
23: doTryCatch(return(expr), name, parentenv, handler)
24: tryCatchOne(expr, names, parentenv, handlers[[1L]])
25: tryCatchList(expr, classes, parentenv, handlers)
26: tryCatch(expr, error = function(e) {    call <- conditionCall(e)    if (!is.null(call)) {        if (identical(call[[1L]], quote(doTryCatch)))             call <- sys.call(-4L)        dcall <- deparse(call)[1L]        prefix <- paste("Error in", dcall, ": ")        LONG <- 75L        msg <- conditionMessage(e)        sm <- strsplit(msg, "\n")[[1L]]        w <- 14L + nchar(dcall, type = "w") + nchar(sm[1L], type = "w")        if (is.na(w))             w <- 14L + nchar(dcall, type = "b") + nchar(sm[1L],                 type = "b")        if (w > LONG)             prefix <- paste0(prefix, "\n  ")    }    else prefix <- "Error : "    msg <- paste0(prefix, conditionMessage(e), "\n")    .Internal(seterrmessage(msg[1L]))    if (!silent && identical(getOption("show.error.messages"),         TRUE)) {        cat(msg, file = stderr())        .Internal(printDeferredWarnings())    }    invisible(structure(msg, class = "try-error", condition = e))})
27: try(suppressPackageStartupMessages(library(pkg_name, lib.loc = lib,     character.only = TRUE, logical.return = TRUE)))
28: tools:::.test_load_package("mypackage", "/home/user/R/x86_64-pc-linux-gnu-library/3.3")
An irrecoverable exception occurred. R is aborting now ...
Segmentation fault (core dumped)
ERROR: loading failed

任何人(@DirkEddelbuettel,我的 Rcpp 大师)都可以告诉我我做错了什么吗?

最佳答案

好吧,您使用的是 2012 年的指南。有时情况会发生变化。我建议使用模块查看当前包。

这里不再需要

.onLoad<-function(libname, pkgname){
    require("methods")
    loadRcppModules()
}

大约从 2013 年开始,我们所要求的只是 R 中的单个 loadModule("moduleName", TRUE),实际上是任何 R 文件。

例如RcppCNPy packageR/目录的全部内容这是

edd@max:~/git/rcppcnpy(master)$ cat R/*.R 

loadModule("cnpy", TRUE)


edd@max:~/git/rcppcnpy(master)$ 

因为包定义了一个单独的模块cnpy

同样,我们不再需要 DESCRIPTION 中的 RcppModules: ... 行。

最后,Rcpp 包本身包含一个完整的工作包,其中包含用于其自身单元测试的模块。你也可以看看那个。

编辑:您可能还需要 importFrom(Rcpp, evalCpp)NAMESPACE 中的类似内容,这将实例化 Rcpp。

关于c++ - 使用 Rcpp 创建模块时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40140584/

相关文章:

C++ - 在 foreach 中复制 vector 给出 "No matching function to call for std::vector<int>::push_back(std::vector<int>&)"

r - 从终端安全地向 R 应用程序提供密码的方法?

r - 将数字转换为特殊格式

c++ - 是否可以在 [win-builder](http ://win-builder. r-project.org/) 上构建一个使用 Rcpp 和 Boost.Thread 的 R 包?

r - 使用 Rcpp 从 C++ 调用 R 函数

c++ - R CMD SHLIB 使用 RcppArmadillo 编译错误

c++ - C++ sans cmath库中的GCD函数

c++:定义可以用作可增长数组的函数

c++ - vector 中的不可复制元素

python - 在 Python、NumPy 和 R 中创建相同的随机数序列