刚刚介绍了如何调用C,那么C++可以吗?R本来是只认C代码的,当然如果你使用C++代码编译也可以通过,但是R却无法调用它。
如果想在R中调用C++代码,那就必须使用Rcpp。使用Rcpp非常简单。
首先我们还是来一个hello qiuworld吧。
首先看一眼目录结构。
·- mypackage |- DESCRIPTION |- NAMESPACE ·- R | |- rcpp_hello_world.R ·- man | |- mypackage-package.Rd |- rcpp_hello_world.Rd ·- src |- Makevars |- Makevars.win |- rcpp_hello_world.cpp |- rcpp_hello_world.h |
在rcpp_hello_world.R中写入
rcpp_hello_world <- function(){ .Call( "rcpp_hello_world", PACKAGE = "mypackage" ) } |
在rcpp_hello_world.h中写入
#ifndef RCPP_HELLO_WORLD_H_H #define RCPP_HELLO_WORLD_H_H #include <Rcpp.h> RcppExport SEXP rcpp_hello_world() ; #endif |
在
rcpp_hello_world.cpp中写入
#include "rcpp_hello_world.h" SEXP rcpp_hello_world(){ using namespace Rcpp ; CharacterVector x = CharacterVector::create( "hello", "qiuworld" ) ; NumericVector y = NumericVector::create( 0.0, 1.0 ) ; List z = List::create( x, y ) ; return z ; } |
在DESCRIPTION文件中写入
Package: helloCpp Type: Package Title: What the package does (short line) Version: 1.0 Date: 2012-06-28 Author: Who wrote it Maintainer: Who to complain to <yourfault@somewhere.net> Description: More about what it does (maybe more than one line) License: What Licence is it under ? Depends: Rcpp (>= 0.9.13) LinkingTo: Rcpp |
在NAMESPACE文件中写入
useDynLib(helloCpp) exportPattern("^[[:alpha:]]+") |
在Makevars中写入
## Use the R HOME indirection to support installations of multiple R version PKG_LIBS = `$(R_HOME)/bin/Rscript -e "Rcpp:::LdFlags()"` |
在Makevars.win中写入
## Use the R HOME indirection to support installations of multiple R version PKG_LIBS = $(shell "${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" -e "Rcpp:::LdFlags()") |
在helloCpp.Rd中写入
\name{helloCpp-package} \alias{helloCpp-package} \alias{helloCpp} \docType{package} \title{ What the package does (short line) } \description{ More about what it does (maybe more than one line) ~~A concise (1-5 lines) description of the package~~ } \details{ \tabular{ll}{ Package: \tab helloCpp\cr Type: \tab Package\cr Version: \tab 1.0\cr Date: \tab 2012-06-28\cr License: \tab What license is it under?\cr } ~~An overview of how to use the package, including the most important~~ ~~functions~~ } \author{ Who wrote it Maintainer: Who to complain to <yourfault@somewhere.net> } \references{ ~~Literature or other references for background information~~ } ~~Optionally other standard keywords, one per line, from file KEYWORDS in~~ ~~the R documentation directory~~ \keyword{ package } \seealso{ ~~Optional links to other man pages, e.g.~~ ~~\code{\link[<pkg>:<pkg>-package]{<pkg>}}~~ } \examples{ %%~~simple examples of the most important functions~~ } |
在rcpp_hello_world.Rd中写入
\name{rcpp_hello_world} \alias{rcpp_hello_world} \docType{package} \title{ Simple function using Rcpp } \description{ Simple function using Rcpp } \usage{ rcpp_hello_world() } \examples{ \dontrun{ rcpp_hello_world() } } |
然后在terminal当前目录下运行,
R CMD check mypackage R CMD build mypackage R CMD INSTALL mypackage_1.0.tar.gz |
而后就可以在R当中运行
> library(mypackage) > rcpp_hello_world() [[1]] [1] "hello" "qiuworld" [[2]] [1] 0 1 |
一切OK.