我怀疑。我知道可以从cpp
文件调用R
函数。
cpp
文件(内部有一个R函数,例如caret
- )编译成.so
或dll
? R
代码。编译后的代码在编译之前调用R解释器还是不需要?提前致谢
答案 0 :(得分:3)
是的,您可以将R嵌入C或C ++应用程序中。 API陈旧,稳定,有些记录,有点笨拙。但它可以做到。
或者您只需使用RInside为您完成所有工作 - 并附带八(8)个不同的示例子目录,其中包含许多工作和工作示例。这是(核心)的一个(有点旧,我们现在可以写得更紧):
#include <RInside.h> // for the embedded R via RInside
#include <iomanip>
int main(int argc, char *argv[]) {
RInside R(argc, argv); // create an embedded R instance
std::string txt = // load library, run regression, create summary
"suppressMessages(require(stats));"
"swisssum <- summary(lm(Fertility ~ . , data = swiss));"
"print(swisssum)";
R.parseEvalQ(txt); // eval command, no return
// evaluate R expressions, and assign directly into Rcpp types
Rcpp::NumericMatrix M( (SEXP) R.parseEval("swcoef <- coef(swisssum)"));
Rcpp::StringVector cnames( (SEXP) R.parseEval("colnames(swcoef)"));
Rcpp::StringVector rnames( (SEXP) R.parseEval("rownames(swcoef)"));
std::cout << "\n\nAnd now from C++\n\n\t\t\t";
for (int i=0; i<cnames.size(); i++) {
std::cout << std::setw(11) << cnames[i] << "\t";
}
std::cout << std::endl;
for (int i=0; i<rnames.size(); i++) {
std::cout << std::setw(16) << rnames[i] << "\t";
for (int j=0; j<cnames.size(); j++) {
std::cout << std::setw(11) << M(i,j) << "\t";
}
std::cout << std::endl;
}
std::cout << std::endl;
exit(0);
}
然后:
edd@max:~/git/rinside/inst/examples/standard(master)$ make rinside_sample3
ccache g++ -I/usr/share/R/include -I/usr/local/lib/R/site-library/Rcpp/include -I/usr/local/lib/R/site-library/RInside/include -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -g -O3 -Wall -pipe -Wno-unused -pedantic -Wall rinside_sample3.cpp -Wl,--export-dynamic -fopenmp -L/usr/lib/R/lib -lR -lpcre -llzma -lbz2 -lz -lrt -ldl -lm -lblas -llapack -L/usr/local/lib/R/site-library/RInside/lib -lRInside -Wl,-rpath,/usr/local/lib/R/site-library/RInside/lib -o rinside_sample3
edd@max:~/git/rinside/inst/examples/standard(master)$ ./rinside_sample3
Call:
lm(formula = Fertility ~ ., data = swiss)
Residuals:
Min 1Q Median 3Q Max
-15.2743 -5.2617 0.5032 4.1198 15.3213
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 66.91518 10.70604 6.250 1.91e-07 ***
Agriculture -0.17211 0.07030 -2.448 0.01873 *
Examination -0.25801 0.25388 -1.016 0.31546
Education -0.87094 0.18303 -4.758 2.43e-05 ***
Catholic 0.10412 0.03526 2.953 0.00519 **
Infant.Mortality 1.07705 0.38172 2.822 0.00734 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 7.165 on 41 degrees of freedom
Multiple R-squared: 0.7067, Adjusted R-squared: 0.671
F-statistic: 19.76 on 5 and 41 DF, p-value: 5.594e-10
And now from C++
Estimate Std. Error t value Pr(>|t|)
(Intercept) 66.9152 10.706 6.25023 1.90605e-07
Agriculture -0.172114 0.0703039 -2.44814 0.0187272
Examination -0.258008 0.253878 -1.01627 0.315462
Education -0.87094 0.183029 -4.75849 2.4306e-05
Catholic 0.104115 0.0352579 2.95297 0.00519008
Infant.Mortality 1.07705 0.38172 2.82157 0.00733572
edd@max:~/git/rinside/inst/examples/standard(master)$
显示
正如我所说,还有几十个例子。不,它并不是神奇地将R编译到您的可执行文件中 - 您需要安装R并调用其共享库。