How do I run Rcpp Hello World?

徘徊边缘 提交于 2019-12-11 06:33:03

问题


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?


回答1:


"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.




回答2:


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



来源:https://stackoverflow.com/questions/16107431/how-do-i-run-rcpp-hello-world

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