从sourceCpp移到带有Rcpp的软件包 [英] Moving from sourceCpp to a package w/Rcpp

查看:72
本文介绍了从sourceCpp移到带有Rcpp的软件包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个 .cpp 文件,可以使用 sourceCpp()进行编译。如预期的那样,将创建相应的R函数,并且代码将按预期工作。

I currently have a .cpp file that I can compile using sourceCpp(). As expected the corresponding R function is created and the code works as expected.

此处是:

#include <Rcpp.h> 
using namespace Rcpp;

// [[Rcpp::export]]
NumericVector exampleOne(NumericVector vectorOne, NumericVector vectorTwo){

    NumericVector outputVector = vectorOne + vectorTwo; 

    return outputVector;
}

我现在正在使用 Rcpp 。因此,我使用rStudio创建了骨骼,并开始研究如何进行转换。

I am now converting my project over to a package using Rcpp. So I created the skeleton with rStudio and started looking at how to convert things over.

在Hadley的关于Cpp的优秀入门书,他在在程序包中使用Rcpp部分中说:

In Hadley's excellent primer on Cpp, he says in section "Using Rcpp in a Package":


如果您的软件包使用Rcpp :: export属性,则需要软件包构建过程中的另一步骤。 compileAttributes函数在程序包中的源文件中扫描Rcpp :: export属性,并生成将函数导出到R所需的代码。

If your packages uses the Rcpp::export attribute then one additional step in the package build process is requried. The compileAttributes function scans the source files within a package for Rcpp::export attributes and generates the code required to export the functions to R.

您应在任何时候重新运行compileAttributes添加,删除功能或更改其签名。请注意,如果使用RStudio或devtools构建软件包,则此步骤会自动发生。

You should re-run compileAttributes whenever functions are added, removed, or have their signatures changed. Note that if you build your package using RStudio or devtools then this step occurs automatically.

因此,看起来像用 sourceCpp()应该可以像在软件包中一样正常工作。

So it looks like the code that compiled with sourceCpp() should work pretty much as is in a package.

我创建了相应的R文件。

I created the corresponding R file.

exampleOne <- function(vectorOne, vectorTwo){
    outToR <- .Call("exampleOne", vectorOne, vectorTwo, PACKAGE ="testPackage")
    outToR
}

然后我(re )构建了程序包,然后出现以下错误:

Then I (re)built the package and I get this error:

.Call( exampleOne,vectorOne,vectorTwo,PACKAGE = voteR)中的错误:
C符号名 exampleOne不在软件包 testPackage的DLL中

有人对它有一个想法吗?

我应该注意,使用sourceCpp()编译的代码然后在程序包中使用它时,我还需要做什么?我读过:写一个包裹给我们es Rcpp http://cran.rstudio.com/web/packages/Rcpp/vignettes /Rcpp-package.pdf 并了解此处介绍的基本结构。但是,在查看 RcppExamples 源代码之后,看起来小插图中的结构与示例包中使用的结构并不完全相同。例如,没有使用.h文件。小插图或源代码也均不使用[[Rcpp :: export]]属性。这一切都使得很难准确地找到我的错误所在。

I should note that I have read: "Writing a package that uses Rcpp" http://cran.rstudio.com/web/packages/Rcpp/vignettes/Rcpp-package.pdf and understand the basic structure presented there. However, after looking at the RcppExamples source code, it appears that the structure in the vignettes is not exactly the same as that used in the example package. For example there are no .h files used. Also neither the vignette nor the source code use the [[Rcpp::export]] attribute. This all makes it difficult to track down exactly where my error is.

推荐答案

这是我的经历如何从使用sourceCpp()转到使用Rcpp的程序包。如果有错误,请随时进行编辑或让我知道,我将对其进行编辑。

Here is my "walk through" of how to go from using sourceCpp() to a package that uses Rcpp. If there is an error please feel free to edit this or let me know and I will edit it.

[注意:我强烈建议在此过程中使用RStudio。]

[NOTE: I HIGHLY recommend using RStudio for this process.]

所以您有sourceCpp()事情发生了,现在您需要构建一个程序包。这并不难,但可能会有些棘手,因为有关使用Rcpp构建软件包的信息来自您想要使用任何R软件包的详尽文档(但作为新手,这是您头上要想的),以及对新手敏感的信息。简介(可能遗漏了您需要的详细信息)。

So you have the sourceCpp() thing down pat and now you need to build a package. This is not hard, but can be a bit tricky, because the information out there about building packages with Rcpp ranges from the exhaustive thorough documentation you want with any R package (but that is above your head as a newbie), and the newbie sensitive introductions (that may leave out a detail you happen to need).

在这里,我使用 oneCpp.cpp twoCpp.cpp 作为将在包中使用的两个.cpp文件的名称。

Here I use oneCpp.cpp and twoCpp.cpp as the names of two .cpp files you will use in your package.

这是我的建议:

A。首先,我假设您有一个 theCppFile.cpp 的版本,可以使用sourceCpp()进行编译,并且可以按预期运行。这不是必须的,但是如果您不熟悉Rcpp OR软件包,最好先确保您的代码在这种简单的情况下可以工作,然后再移至下面的更复杂的情况。

A. First I assume you have a version of theCppFile.cpp that compiles with sourceCpp() and works as you expect it to. This is not a must, but if you are new to Rcpp OR packages, it is nice to make sure your code works in this simple situation before you move to the more complicated case below.

B。现在,使用 Rcpp.package.skeleton()或使用RStudio中的Project> Create Project> Package w / Rcpp向导构建软件包(强烈建议)。您可以在Rcpp.package.skeleton()的详细信息。 nofollow noreferrer> hadley / devtools Rcpp属性小插图。使用Rcpp编写软件包的完整文档位于编写使用Rcpp 的程序包,但是此程序假定您相当了解C ++,并且不使用新的属性方式进行Rcpp。但是,如果您打算制作更复杂的软件包,这将是无价的。

B. Now build your package using Rcpp.package.skeleton() or use the Project>Create Project>Package w/Rcpp wizard in RStudio (HIGHLY recommended). You can find details about using Rcpp.package.skeleton() in hadley/devtools or Rcpp Attributes Vignette. The full documentation for writing packages with Rcpp is in Writing a package that uses Rcpp, however this one assumes you know your way around C++ fairly well, and does not use the new "Attributes" way of doing Rcpp. It will be invaluable though if you move toward making more complex packages.

您现在应该为软件包提供一个目录结构,如下所示:

You should now have a directory structure for your package that looks something like this:

yourPackageName
- DESCRIPTION
- NAMESPACE
- \R\
    - RcppExports.R 
- Read-and-delete-me
- \man\
    - yourPackageName-package.Rd
- \src\
    - Makevars
    - Makevars.win
    - oneCpp.cpp 
    - twoCpp.cpp
    - RcppExports.cpp

一切都设置好后,如果使用RStudio,请执行 Build& Reload;如果您不在RStudio中,请执行 compileAttributes()

Once everything is set up, do a "Build & Reload" if using RStudio, or compileAttributes() if you are not in RStudio.

C。现在,您应该在 \R 目录中看到一个名为 RcppExports.R 的文件。打开并签出。在 RcppExports.R 中,您应该看到<$ c中所有 .cpp 文件的R包装函数。 $ c> \src 目录。

C. You should now see in your \R directory a file called RcppExports.R. Open it and check it out. In RcppExports.R you should see the R wrapper functions for all the .cpp files you have in your \src directory. Pretty sweet, eh?.

D)尝试与您在 theCppFile.cpp 。它行得通吗?

D) Try out the R function that corresponds to the function you wrote in theCppFile.cpp. Does it work? If so move on.

E)现在,您只需添加新的 .cpp 文件,例如 otherCpp.cpp \src 目录中。然后,您只需重建软件包,即可生成R包装器,并为您添加到 RcppExports.R 中。在RStudio中,这只是构建菜单中的构建和重新加载。如果您不使用RStudio,则应运行 compileAttributes()

E) You can now just add new .cpp files like otherCpp.cpp to the \src directory as you create them. Then you just have to rebuild the package, and the R wrappers will be generated and added to RcppExports.R for you. In RStudio this is just "Build & Reload" in the Build menu. If you are not using RStudio you should run compileAttributes()

这篇关于从sourceCpp移到带有Rcpp的软件包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆