我试图在Rcpp函数中打开一个文件,所以我需要文件名作为char*或std :: string.
到目前为止,我尝试过以下方法:
#include <Rcpp.h>
#include <boost/algorithm/string.hpp>
#include <fstream>
#include <string>
RcppExport SEXP readData(SEXP f1) {
Rcpp::CharacterVector ff(f1);
std::string fname = Rcpp::as(ff);
std::ifstream fi;
fi.open(fname.c_str(),std::ios::in);
std::string line;
fi >> line;
Rcpp::CharacterVector rline = Rcpp::wrap(line);
return rline;
}
Run Code Online (Sandbox Code Playgroud)
但显然,因为我得到编译时错误as不起作用Rcpp::CharacterVector.
foo.cpp: In function 'SEXPREC* readData(SEXPREC*)':
foo.cpp:8: error: no matching function for call to 'as(Rcpp::CharacterVector&)'
make: *** [foo.o] Error 1
Run Code Online (Sandbox Code Playgroud)
有没有一种简单的方法从参数中获取字符串或以某种方式从Rcpp函数参数中打开文件?