gpt4 book ai didi

r - 从同一包中的 R 代码调用 Rcpp c++​​ 函数

转载 作者:行者123 更新时间:2023-12-04 01:30:36 24 4
gpt4 key购买 nike

我想用 C++ 和 R 代码构建一个 Rcpp 包。仅使用 C++ 代码(删除了 R 代码),所有内容都可以编译并运行良好,并且在构建和加载导出函数后可以按预期调用

<packageName>::<functionName>()

但是,当包含 R 代码和对 C++ 代码的调用时,我在构建包时遇到找不到对象的错误:

R CMD build <packageName>
[...]
** R
** byte-compile and prepare package for lazy loading
Error in <functionName>() :
object '_<packageName>_<functionName>' not found
Error: unable to load R code in package '<packageName>'

错误发生在C++代码编译成功后。

<functionName>()

映射到

_<packageName>_<functionName>

像往常一样在 R/RcppExports.R 中,但它似乎不能在构建时由 R 代码加载。

包的结构和往常一样:

<packageName>
├── DESCRIPTION
├── man
│   ├── <functionName>.rd
│   └── <packageName>-package.rd
├── NAMESPACE
├── R
│   ├── <RCodeFileName>.R
│   └── RcppExports.R
├── README.md
└── src
├── <C++CodeFileName>.cpp
├── <C++CodeFileName>.o
├── <packageName>.so
├── RCppExports.cpp
├── RCppExports.o
└── symbols.rds

编译包时生成.o.so文件的地方。

R文件直接调用C++文件中的函数:

[other stuff]
[...]
<functionName>()

NAMESPACE 文件也照常:

useDynLib(<packageName>, .registration=TRUE)
importFrom(Rcpp, evalCpp)
exportPattern("^[[:alpha:]]+")

这似乎是一个非常简单直接的问题(从 Rcpp 包中的 R 代码调用 C++ 代码)。尽管如此,我似乎无法找到任何说明应该如何完成的指示。

编辑(2020 年 4 月 8 日):buildINSTALL

在每种情况下都会出现这个问题

R CMD INSTALL <packageName>_<version>.tar.gz

如果包含帮助文件

man/<packageName>-package.Rd

被构建以便它强制安装包来处理帮助页面错误已经发生在

R CMD build <packageName>

如上所述。

编辑(2020 年 4 月 8 日):使用 Rcpp.package.skeleton() 重现的步骤(无论如何在类 Unix 系统上)

Rscript -e 'Rcpp::Rcpp.package.skeleton("demo20200408")'
echo 'rcpp_hello_world()' > errorDemo/R/example.R
R CMD INSTALL errorDemo

最佳答案

你可能想放慢速度。 Rcpp 包本身通过Rcpp.package.skeleton() 函数附带了一个演示包生成器。运行它!

逐个比较您所拥有的。

第二个生成器例如内置于 RStudio 中,可在文件 -> 新建项目 -> 新建目录 -> Package with Rcpp 菜单选项下使用。

否则,很难说。你对你的函数名做了什么奇怪的事吗?因为它们被映射到 R C++,所以您受到两者的限制。 您不能使用点(因为那将是 C++ 中的类方法表示法)。

最后,即使未导出,C++ 函数也应该可以通过三个冒号从安装和加载的包中获得, mypkg:::myFun().

最后,快速演示:

创建它
edd@rob:/tmp$ Rscript -e 'Rcpp::Rcpp.package.skeleton("demo20200408")'
Creating directories ...
Creating DESCRIPTION ...
Creating NAMESPACE ...
Creating Read-and-delete-me ...
Saving functions and data ...
Making help files ...
Done.
Further steps are described in './demo20200408/Read-and-delete-me'.

Adding Rcpp settings
>> added Imports: Rcpp
>> added LinkingTo: Rcpp
>> added useDynLib directive to NAMESPACE
>> added importFrom(Rcpp, evalCpp) directive to NAMESPACE
>> added example src file using Rcpp attributes
>> added Rd file for rcpp_hello_world
>> compiled Rcpp attributes
edd@rob:/tmp$
安装
edd@rob:/tmp$ R CMD INSTALL demo20200408 
* installing to library ‘/usr/local/lib/R/site-library’
* installing *source* package ‘demo20200408’ ...
** 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 rcpp_hello_world.cpp -o rcpp_hello_world.o
ccache g++ -Wl,-S -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o demo20200408.so RcppExports.o rcpp_hello_world.o -L/usr/lib/R/lib -lR
installing to /usr/local/lib/R/site-library/00LOCK-demo20200408/00new/demo20200408/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 (demo20200408)
edd@rob:/tmp$
运行它
edd@rob:/tmp$ Rscript -e 'library(demo20200408); rcpp_hello_world()'
[[1]]
[1] "foo" "bar"

[[2]]
[1] 0 1

edd@rob:/tmp$
并添加一个 R 函数并调用它
edd@rob:/tmp$ echo 'r_hello_world <- function() cat("hi there\n")' > demo20200408/R/foo.R
edd@rob:/tmp$ R CMD INSTALL demo20200408
* installing to library ‘/usr/local/lib/R/site-library’
* installing *source* package ‘demo20200408’ ...
** using staged installation
** libs
make: Nothing to be done for 'all'.
installing to /usr/local/lib/R/site-library/00LOCK-demo20200408/00new/demo20200408/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 (demo20200408)
edd@rob:/tmp$ Rscript -e 'library(demo20200408); r_hello_world()'
hi there
edd@rob:/tmp$
尽管 OP 声称仍然没有问题

在下面的最新评论中声称无法调用生成的函数。那是错误的。

edd@rob:/tmp$ editor demo20200408/R/foo.R    # subst. fave editor here
edd@rob:/tmp$ cat demo20200408/R/foo.R
r_hello_world <- function() {
cat("hi there\n")
ignored <- rcpp_hello_world()
NULL
}
edd@rob:/tmp$ R CMD INSTALL demo20200408
* installing to library ‘/usr/local/lib/R/site-library’
* installing *source* package ‘demo20200408’ ...
** using staged installation
** libs
make: Nothing to be done for 'all'.
installing to /usr/local/lib/R/site-library/00LOCK-demo20200408/00new/demo20200408/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 (demo20200408)
edd@rob:/tmp$ Rscript -e 'library(demo20200408); r_hello_world()'
hi there
NULL
edd@rob:/tmp$

关于r - 从同一包中的 R 代码调用 Rcpp c++​​ 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61106608/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com