August Karlstrom
August Karlstrom

Reputation: 11377

How do I run Rcpp Hello World?

OK, so I have created an R package foo with function Rcpp.package.skeleton. I have also compiled the Hello World C++ file with

R CMD SHLIB foo/src/rcpp_hello_world.cpp

However, when I call rcpp_hello_world I get an error:

> source("foo/R/rcpp_hello_world.R")
> rcpp_hello_world()
Error in .Call("rcpp_hello_world", PACKAGE = "foo") : 
  "rcpp_hello_world" not available for .Call() for package "foo"

Any clues?

Upvotes: 1

Views: 1700

Answers (2)

SmallChess
SmallChess

Reputation: 8126

To run the "hello world" example, do the following:

  1. Start R and install the Rcpp package by:

    install.packages('Rcpp')

  2. Generate Rcpp template, in R:

    Rcpp.package.skeleton("mypackage")

  3. Next, create an archive for the package:

    R CMD build mypackage

  4. Exit R. You should see a folder "mypackage" generated. Type the following to check the package:

    R CMD check mypackage

  5. Now, you will see an archive mypackage_1.0.tar.gz. Install it:

    R CMD INSTALL mypackage_1.0.tar.gz

  6. Let's run the package in R. Start R and do the following:

    library('mypackage')

    rcpp_hello_world() # Try the C++ function generated in the template

    [[1]] [1] "foo" "bar"

    [[2]] [1] 0 1

Upvotes: 1

Dirk is no longer here
Dirk is no longer here

Reputation: 368519

"Package skeleton" implies that you are supposed to follow the creation of a (simple, skeleton) package with (optionally) building the package (into a tar.gz) as well as installing it.

Once installed you can load it and then you can in fact execute the new function.

Alternatively, you can work on the fly via Rcpp Attributes and/or the inline package.

Upvotes: 6

Related Questions