【问题标题】:RcppEigen package error: not available for .Call() for packageRcppEigen 包错误:不适用于包的 .Call()
【发布时间】:2021-09-01 17:49:20
【问题描述】:

我有一个RcppEigen package 成功地Rcpp::sourceCpp()s、devtools::check()s 和devtools::document()s,但是当我尝试运行一个简单的示例时出现以下错误:

Error in .Call("_pfexamplesinr_svolApproxLL", PACKAGE = "pfexamplesinr",  : 
  "_pfexamplesinr_svolApproxLL" not available for .Call() for package "pfexamplesinr"

错误是由以下最少的代码生成的

devtools::install_github("tbrown122387/pfexamplesinr@cffe989")
numTime <- 3
numParts <- 500 # must agree with #define NP in likelihoods.cpp
u <- rnorm(numTime*(numParts+1))
params <- c(.9, 1, .1) # -1 < phi < 1, beta, sigma > 0
pfexamplesinr::svolApproxLL(rnorm(numTime), params, u)

This question 暗示它可能是src/Makevars 中缺少的PKG_LIBS,但my src/Makevars

## With Rcpp 0.11.0 and later, we no longer need to set PKG_LIBS as there is
## no user-facing library. The include path to headers is already set by R.
#PKG_LIBS = 

【问题讨论】:

  • 我必须在 cpp 文件中的svolApproxLL 之前将三行添加到//' @export ; //' @useDynLib pfexamplesinr ; //' @importFrom Rcpp sourceCpp(没有;),重新运行文档并再次构建您上面的代码运行并输出一个数字
  • 是的。并运行compileAttributes() 将其放入R 文件中,并使用完整参数运行roxygenize() 以重写NAMESPACE。
  • 总而言之,您可以使用 RcppEigen.package.skeleton()(和/或 RStudio 中相应的 'New Project -> Package w/RcppEigen'),但事实并非如此您确实创建的骨架并不能确保您的包(目标代码)库的实际加载。类似于错误的PKG_LIB,但正如您所指出的,我们不再需要它,不同。

标签: c++ r rcpp


【解决方案1】:

您到底在做什么,您到底遇到了什么什么错误?它在这里工作(除了一个警告):

edd@rob:~/git$ git clone git@github.com:eddelbuettel/pfexamplesinr.git
Cloning into 'pfexamplesinr'...
remote: Enumerating objects: 141, done.
remote: Counting objects: 100% (141/141), done.
remote: Compressing objects: 100% (82/82), done.
remote: Total 141 (delta 65), reused 120 (delta 44), pack-reused 0
Receiving objects: 100% (141/141), 46.75 KiB | 1.42 MiB/s, done.
Resolving deltas: 100% (65/65), done.
edd@rob:~/git$ cd pfexamplesinr/
edd@rob:~/git/pfexamplesinr(master)$ install.r
* installing *source* package found in current working directory ...
* installing *source* package ‘pfexamplesinr’ ...
** using staged installation
** libs
ccache g++  -std=gnu++14 -I"/usr/share/R/include" -DNDEBUG  -I'/usr/local/lib/R/site-library/Rcpp/include' -I'/usr/local/lib/R/site-library/RcppEigen/include'    -fpic  -g -O3 -Wall -pipe -pedantic -Wno-misleading-indentation -Wno-unused -Wno-ignored-attributes -Wno-class-memaccess -c RcppExports.cpp -o RcppExports.o
ccache g++  -std=gnu++14 -I"/usr/share/R/include" -DNDEBUG  -I'/usr/local/lib/R/site-library/Rcpp/include' -I'/usr/local/lib/R/site-library/RcppEigen/include'    -fpic  -g -O3 -Wall -pipe -pedantic -Wno-misleading-indentation -Wno-unused -Wno-ignored-attributes -Wno-class-memaccess -c likelihoods.cpp -o likelihoods.o
In file included from likelihoods.cpp:2:
resamplers.h: In member function ‘void pf::resamplers::sys_hilb_resampler<nparts, dimx, num_hilb_bits, float_t>::resampLogWts(pf::resamplers::sys_hilb_resampler<nparts, dimx, num_hilb_bits, float_t>::arrayVec&, pf::resamplers::sys_hilb_resampler<nparts, dimx, num_hilb_bits, float_t>::arrayFloat&, const usvr&) [with long unsigned int nparts = 500; long unsigned int dimx = 1; long unsigned int num_hilb_bits = 5; float_t = double]’:
resamplers.h:1089:36: warning: ‘idx’ may be used uninitialized in this function [-Wmaybe-uninitialized]
 1089 |         tmpPartics[i] = sortedParts[idx];
      |                         ~~~~~~~~~~~^
ccache g++ -std=gnu++14 -Wl,-S -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -flto=auto -Wl,-z,relro -o pfexamplesinr.so RcppExports.o likelihoods.o -L/usr/lib/R/lib -lR
installing to /usr/local/lib/R/site-library/00LOCK-pfexamplesinr/00new/pfexamplesinr/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 (pfexamplesinr)
edd@rob:~/git/pfexamplesinr(master)$ 

我假设它需要修复;看起来好像不需要。

可能需要调整的是如何构建和安装包。我使用了我的littler 包中的一些包装器,但是一个普通的R CMD INSTALL ... 也可以。我现在向您发送了一个简单的拉取请求,以抑制 idx 上的警告。

编辑:我现在看到了。您从一个普通包开始,而不是 Rcpp(甚至 RcppEigen 包),因此 NAMESPACE 文件缺少 dynLib() 条目等。

编辑 2: 您的 NAMESPACE 不足。更快的方法是在 e.g. here

中使用标准格式
useDynLib(pfexamplesinr)
import(RcppEigen)
importFrom(Rcpp, evalCpp)

## export all regularly named functions
## (but allow for private functions whose name starts with a dot).name <- function(...)
exportPattern("^[[:alpha:]]+")

(较长的形式通过roxygen2 生成前三行;我相信你可以添加它。)

完成后,您的示例将运行:

edd@rob:~/git/pfexamplesinr(master)$ cat /tmp/test.R 
library(pfexamplesinr)
numTime <- 3
numParts <- 500
u <- rnorm(numTime*(numParts+1))
params <- c(.9, 1, .1) # -1 < phi < 1, beta, sigma > 0
svolApproxLL(rnorm(numTime), params, u)
edd@rob:~/git/pfexamplesinr(master)$ Rscript /tmp/test.R 
[1] -4.98311
edd@rob:~/git/pfexamplesinr(master)$ 

我将修改我的 PR 以添加此内容。

【讨论】:

  • 谢谢您,先生。我想我确实是从专门的骨架函数开始的,但不知何故我在某个地方丢失了 NAMESPACE。我不相信我调用了正确的函数来生成它,所以我会学习
  • 要做到这一点实际上令人惊讶,而且正如您注意到的错误消息并没有立即提供帮助。我最近扩展了我的帮助程序脚本 kitten.r(来自我经常使用的 littler 包)来调用 RcppArmadillo 和 RcppEigen 帮助程序,但是仍然没有 roxygen2 支持。 (@user20650 在评论中写的是正确的;我仍然经常对小包使用“普通”方法。)你带着包到达那里,让我们知道(提示:rcpp-devel 邮件也很好;- ) ) 如果我们能提供更多帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
  • 2017-06-25
  • 1970-01-01
  • 2023-01-03
相关资源
最近更新 更多