Rcpp和RcppArmadillo编写的函数打包成R包并安装
Rcpp和RcppArmadillo编写的函数打包成R包并安装
环境
按要求安装rtools,rcpp和rcpparmadillo库
将rtools和R添加到系统路径
R的默认路径从开始菜单一路导航就能找到,rtools的默认路径一般在C盘,将...\Rtools\bin
加入系统变量中的path变量里面。
创建一个R包
首先我们应该已经有一堆用rcpp和rcpparmodillo写的cpp函数。
这里我们要做的只是把他们写进包里面
run the following code to create the skeletion of the package.
library(Rcpp)
library(RcppArmadillo)
RcppArmadillo.package.skeleton("name_of_package", path = "path_of_the_package_you_want_to_build")
- put the cpp file into
...\path_of_the_package_you_want_to_build\src\
- run
setwd(path_of_the_package_you_want_to_build)
- run
Rcpp::compileAttributes()
- Now we are ready to build the package!
Build the package
- open the cmd 不需要管理员权限
- run
R CMD build path_of_the_package_you_want_to_build 不需要引号!
- run
R CMD INSTALL name_of_package_1.0.tar.gz
- Open R and try
library(name_of_package)
. If no error happens, the package is succeesfully built and installed!
参考的网页是https://www.r-bloggers.com/2018/12/building-an-r-package-that-includes-rcpparmadillo-code/