[英]Rcpp: C++ functions don't work in R package
I have some functions in R and I re-coded them by Rcpp. 我在R中有一些功能,并由Rcpp重新编码。 Each of those functions has a stand-along .cpp file.
每个功能都有一个独立的.cpp文件。 One function called
add_inflow()
. 一个函数称为
add_inflow()
。 Previously, I put all cpp functions on my desktop and used Rcpp::sourceCpp("add_inflow.cpp")
. 以前,我将所有cpp函数都放在桌面上,并使用了
Rcpp::sourceCpp("add_inflow.cpp")
。 Then, this c++ function could give me an output value by plugging parameters. 然后,此c ++函数可以通过插入参数为我提供输出值。
Then I want to embed them into my R package called stormwindmodel
, by following Compiled Code, R Packages, Hadley 然后,我想按照编译代码,R包,Hadley将它们嵌入到我的名为
stormwindmodel
R包中
First, I run devtools::use_rcpp()
, then moved all cpp functions under src
file. 首先,我运行
devtools::use_rcpp()
,然后将所有cpp函数移到src
文件下。 Then, I clicked build&reload button and it was succesfully done. 然后,我单击“构建和重新加载”按钮,此操作成功完成。 At this point, I found the original R functions were in the Environmental Panel but didn't see my cpp functinos.
在这一点上,我发现原始的R函数在环境面板中,但是没有看到我的cpp函数。 Then I run
load_all
, and this time cpp functinos showed up. 然后我运行
load_all
,这一次出现了cpp functinos。 However, when I run add_inflow_Cpp()
function, Rstudio gave me this output: 但是,当我运行
add_inflow_Cpp()
函数时,Rstudio给了我以下输出:
Error in .Call("stormwindmodel_add_forward_speed_Cpp", PACKAGE = "stormwindmodel", :
"stormwindmodel_add_forward_speed_Cpp" not available for .Call() for package "stormwindmodel"
Did I miss any steps? 我错过任何步骤了吗? Any suggestion to me?
有什么建议吗?
If the question was not of good enough quality then please give me feedback, I will edit it as soon as possible. 如果问题质量不够好,请给我反馈,我将尽快对其进行编辑。
You are likely missing the useDynLib(<pkg>)
entry in your NAMESPACE
file. 您可能会丢失
NAMESPACE
文件中的useDynLib(<pkg>)
条目。 If you're using Roxygen and following the examples in the book, you need to include the following content in an R file (the best guess at this point is you missed this step): 如果您使用的是Roxygen并遵循本书中的示例,则需要在R文件中包含以下内容(目前最好的猜测是您错过了此步骤):
#' @useDynLib your-package-name
#' @importFrom Rcpp sourceCpp
NULL
The @useDynLib <pkg>
Roxygen directive instructs the roxygen2
package to include a useDynLib(<pkg>)
in the NAMESPACE
file whenever you re-document the package. @useDynLib <pkg>
Roxygen指令指示roxygen2
软件包在每次重新记录软件包时在NAMESPACE
文件中包含useDynLib(<pkg>)
。
Did you remember to add the associated lines above to an R file in your package in the R folder (eg at R/package-init.R
), and re-document the package after adding that? 您是否记得将上述关联的行添加到R文件夹中包中的R文件中(例如,在
R/package-init.R
),并在添加后重新记录该包? If you've done everything correctly, you should see useDynLib(<pkg>)
added to the NAMESPACE
file, with <pkg>
replaced by the actual name of your package. 如果正确完成所有操作,则应该看到
useDynLib(<pkg>)
添加到NAMESPACE
文件中,而<pkg>
被替换为包的实际名称。
It should be noted that devtools::use_rcpp()
does not do this automatically for you -- after running the function, it instructs you that you need to do this step manually: 应当指出的是,
devtools::use_rcpp()
不会自动为您做到这一点-运行的功能之后,它会指示你,你需要手动执行该步骤:
> devtools::use_rcpp()
Adding Rcpp to LinkingTo and Imports
* Creating `src/`.
* Ignoring generated binary files.
Next, include the following roxygen tags somewhere in your package:
#' @useDynLib sparklyr
#' @importFrom Rcpp sourceCpp
NULL
Then run document()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.