利用Rcpp和RcppArmadillo创建R包
来源:互联网 发布:詹姆斯退役数据预测 编辑:程序博客网 时间:2024/06/10 03:38
1. 预先准备
Windows下需要安装Rtools,R中装好Rcpp和RcppArmadillo。创建C++源文件func.cpp
,自定义头文件test_h.h
。
源文件示例func.cpp
// [[Rcpp::depends(RcppArmadillo)]]// [[Rcpp::plugins(cpp11)]]#include <RcppArmadillo.h>#include <vector>#include "./test_h.h"using namespace Rcpp;using namespace std;// using namespace arma;// [[Rcpp::export]]RObject func(){ arma::cube A(3,4,5,arma::fill::randu); std::cout<<CONDIT<<std::endl; return wrap(A);}
// [[Rcpp::depends(RcppArmadillo)]]
:用于指明需要使用RcppArmadillo。// [[Rcpp::plugins(cpp11)]]
:指明需要使用C++11。#include "./test_h.h"
:表示使用第三方头文件。第三方头文件需要用双引号""
包括起来,并加上.h
。./
表示在当下文件夹(src)下搜寻文件。// using namespace arma;
不一定有用。如果要用Armadillo的数据结构,在其之前需指明arma::
。func()
函数将可以直接在R中调用。
头文件示例test_h.h
#include <iostream>#define CONDIT 1000
2. 创建R包步骤
新建R Package
选择Package w/Rcpp, 并添加源文件。或者建立包以后,手动复制到src文件夹下。
R包的文件结构
修改DESCRIPTION
文件
将RcppArmadillo添加进Imports
和LinkingTo
中。
Package: RcppPackTestType: PackageTitle: What the Package Does (Title Case)Version: 0.1.0Author: Who wrote itMaintainer: The package maintainer <yourself@somewhere.net>Description: More about what it does (maybe more than one line) Use four spaces when indenting paragraphs within the Description.License: What license is it under?Encoding: UTF-8LazyData: trueImports: Rcpp (>= 0.12.11), RcppArmadilloLinkingTo: Rcpp, RcppArmadillo
Build & Reload 建立包
3. C++11标准问题
如果要使用C++11标准,第一种方法是在Makevars文件中添加如下代码:
CXX = g++-4.8.1PKG_CXXFLAGS = -std=c++11
第二种方法是在.cpp
文件前添加// [[Rcpp::plugins(cpp11)]]
阅读全文
0 0
- 利用Rcpp和RcppArmadillo创建R包
- Linux Ubuntu下g++编译器协同C++(Rcpp RcppArmadillo)与R包开发之编译
- Redhat Linux 5.9 安装R包RcppArmadillo时报错
- DESeq2 install --- 如何安装R包( ‘RcppArmadillo’)?
- Rstudio中建立包含Rcpp的R包
- Rcpp实现R与C++的无缝连接
- Using R — Callling C code with Rcpp
- windows系统下R升级中Rcpp无法载入解决办法
- 利用R包ggmap进行空间可视化
- R利用RJDBC包连接数据库
- R语言利用neuralnet包训练神经网络
- [R语言] 加载和安装R包
- R语言中帮助和R包
- [R语言] 加载和安装R包
- 展示如何用R处理稀疏矩阵和利用GLMNET包
- android 创建项目后R包问题
- Ubuntu下运行Rcpp代码时R报dir.exsits()不存在的错误
- R语言利用igraph和networkD3包快速入门做出炫酷的社交网络图等几类图。
- HDU 5212 Code(容斥 或 莫比乌斯反演)
- git最常用!最实用!最基本的命令解析!绝对够用!
- 希尔排序(shell sort)
- tf.py_func errors in freeze_graph
- SPOJ 10628. Count on a tree (树上第k大,LCA+主席树)
- 利用Rcpp和RcppArmadillo创建R包
- 将高德坐标拾取工具放入Element UI 对话框
- MySQL 5.7搭建备库 开启gtid 使用mysqldump
- POJ 2104 -- K-th Number (分桶法和平方分割)
- offsetTop-clientTop-scrollTop-各属性介绍
- Advice for New Graduate Students
- bat 2018自然语言处理校园招聘的要求
- 如何根据数据库文件自动生成整个bean层与service层
- learning