我对如何使用Rcpp包感到困惑。在保存以下.cpp代码之后,什么是使用newRcppVectorExample的R函数的R代码,example.cpp表示?代码是从http://dirk.eddelbuettel.com/code/rcpp.examples.html。R代码使用.cpp文件中的函数,基于Rcpp包
我需要创建一个包吗?我可以直接使用example.cpp吗?例如R CMD SHLIB example.cpp,然后使用dyn.load函数。
#include <RcppClassic.h>
#include <cmath>
RcppExport SEXP newRcppVectorExample(SEXP vector) {
BEGIN_RCPP
Rcpp::NumericVector orig(vector);
Rcpp::NumericVector vec(orig.size());
std::transform(orig.begin(), orig.end(), vec.begin(), ::sqrt);
return Rcpp::List::create(Rcpp::Named("result") = vec,
Rcpp::Named("original") = orig) ;
END_RCPP
}