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")
  1. put the cpp file into ...\path_of_the_package_you_want_to_build\src\
  2. run
setwd(path_of_the_package_you_want_to_build)
  1. run
Rcpp::compileAttributes()
  1. Now we are ready to build the package!

Build the package

  1. open the cmd 不需要管理员权限
  2. run
R CMD build path_of_the_package_you_want_to_build 不需要引号!
  1. run
R CMD INSTALL name_of_package_1.0.tar.gz
  1. 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/


posted @ 2022-02-06 20:08  dawangluo  阅读(459)  评论(0)    收藏  举报