我正在尝试在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;
}
但显然,由于我收到编译时错误,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
有没有一种简单的方法可以从参数中获取字符串或以某种方式从 Rcpp 函数参数中打开文件?