Rcpp: why I can not run the function in my defined package?

こ雲淡風輕ζ 提交于 2020-03-01 01:55:48

问题


I use the following steps to achieve my own package:

1)I try to write a very simple function as follows:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
int foo() {
return 6;
}

2) I use skeleton to make it into a package:

Rcpp.package.skeleton("newpackage",example_code=FALSE,cpp_files=c("New.cpp"))

3) I run cpp with command:

source("~/newpackage/src/New.cpp")

4) run compileAttributes to load the package:

 compileAttributes(pkgdir="/home/tw72/newpackage",verbose = getOption("verbose"))

After this I want to call the function in R:

foo <- function( )
{
.Call("foo",PACKAGE="newpackage")
}

Then the error is:

Error in .Call("foo", PACKAGE = "newpackage") :

"foo" not available for .Call() for package "newpackage"

I met the same problem, but I still can not figure out what happens. Could you help me? What's wrong with my above steps? Thanks.


回答1:


From the top of my head, it looks pretty complete but do try

R> Rcpp.package.skeleton("newpackage",
+                        example_code=FALSE,      ## useful but not required
+                        cpp_files=c("New.cpp"),  ## may not be required 
+                        attributes=TRUE)         ## this is important
R>

as both Rcpp modules and Rcpp attributes need to be turned on.

After that, things should work as you do the required compileAttributes.

Edit: It is even simpler. Just do do the Rcpp.package.skeleton() call I outlined above, that is with the added attributes=TRUE after which you are done -- install the package and test it.



来源:https://stackoverflow.com/questions/16581166/rcpp-why-i-can-not-run-the-function-in-my-defined-package

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!