Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

rcpp

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.

like image 643
user2380245 Avatar asked May 16 '13 07:05

user2380245


People also ask

What is RCPP function?

Description The 'Rcpp' package provides R functions as well as C++ classes which offer a seamless integration of R and C++. Many R data types and objects can be mapped back and forth to C++ equivalents which facilitates both writing of new code as well as easier integration of third-party libraries.

Why use Rcpp?

Rcpp provides a lot of syntactic “sugar” to ensure that C++ functions work very similarly to their R equivalents. In fact, Rcpp sugar makes it possible to write efficient C++ code that looks almost identical to its R equivalent.

What is RCPP sugar?

Rcpp sugar brings a higher-level of abstraction to C++ code written using the Rcpp API. Rcpp sugar is based on expression templates (Abrahams and Gurtovoy, 2004; Vandevoorde and Josuttis, 2003) and provides some 'syntactic sugar' facilities directly in Rcpp.


1 Answers

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.

like image 165
Dirk Eddelbuettel Avatar answered Oct 15 '22 23:10

Dirk Eddelbuettel