C++如何调用R

来源:互联网 发布:牛耳软件学院图片 编辑:程序博客网 时间:2024/05/18 00:48

C++如何调用R

#自定义函数 暂命名为test.Rmyfunction=function(x){    tmp=apply(x,1,mean)}
//test.cpp#include <Rcpp.h>// 必不可少的的头文件using namespace Rcpp;// [[Rcpp::export]]/*上一行的"//[[Rcpp::export]]"是必要的*/NumericVector callFunction(NumericVector x, Function f) {    NumericVector res = f(x);    return res;}
library(Rcpp)#设置当前路径#setwd("D:\\Desktop\\")source("test.R")source("test.cpp")set.seed(0715)x=matrix(rnorm(100),25,4)callFunction(x,myfunction)#或者用内置函数callFunction(x,mean)
原创粉丝点击