1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
struct MyData
{
int un_int;
float un_float;
std::string un_string;
};
int main(int argc, char *argv[])
{
RInside R(argc, argv);
// executer une fonction
R.parseEvalQ("print('hello world!')");
std::cout << std::endl;
// lecture et ecriture d'une variable
R["x"] = 10;
R["y"] = 20;
R.parseEvalQ("z <- x + y");
int z = R["z"];
std::cout << "z:" << z << std::cout;
std::cout << std::endl;
// lecture et écriture d'un vecteur numérique
const double dvec[6] = { 0.1 , 0.1 , 0.1 , 0.1 , 0.3 , 0.3 };
const std::vector<double> v(dvec , dvec+6);
R.assign(v, "v");
SEXP ans;
ans = R.parseEval("v*2");
Rcpp::NumericVector v2(ans);
for (int i=0; i<6; ++i) std::cout << "v2." << i << ":" << v2[i] << std::endl;
std::cout << std::endl;
// lecture/écriture d'un vecteur string
std::vector<std::string> vs;
vs.push_back("string 1");
vs.push_back("string 2");
R.assign(vs, "vs") ;
ans = R.parseEval("size <- length(vs); paste(vs, ' toto'); ");
int size = R["size"];
std::cout << "size:" << size << std::endl;
Rcpp::CharacterVector vs2(ans);
for (int i=0; i<size; ++i) std::cout << "vs2." << i << ":" << vs2[i] << std::endl;
std::cout << std::endl;
// lecture/écriture d'un type complexe
MyData d;
d.un_float = 12.3456;
d.un_int = 12345;
d.un_string = "un test";
R.assign(d.un_float, "d$un_float");
R.assign(d.un_int, "d$un_int");
R.assign(d.un_string, "d$un_string");
R.parseEvalQ("print(d)");
// ouverture d'une library et appel de fonction
R.parseEvalQ(
"library(MassSpecWavelet);"
"ms <- read.table('data.txt', dec='.', sep=' ');"
"mz <- ms[17000:17500,3];"
"size <- length(mz);");
size = R["size"];
std::cout << "size:" << size << std::endl;
Rcpp::NumericVector mz = R["mz"];
std::cout << "mz size:" << mz.size() << std::endl;
for (int i=0; i<mz.size(); ++i) mz[i] += 100.0 * cos(i/10.0);
// enregistrement de l'image vers un png
R.assign(mz, "mz_out");
R.parseEvalQ(
"png('toto.png');"
"plot(mz, type='l', col='red');"
"lines(mz_out, type='l', col='blue');");
//exec complex script
R.parseEvalQ(
"mz_inf = which(ms[,2] > 2080)[1];"
"mz_sup = which(ms[,2] > 2110)[1];"
"exampleMS <- ms[mz_inf:mz_sup, 3];"
"scales <- seq(1, 10, 0.2);"
"wavelet <- 'mexh'; print('A');"
"wCoefs <- cwt(exampleMS, scales = scales, wavelet = wavelet);print('B');"
"wCoefs <- cbind(as.vector(exampleMS), wCoefs);print('C');"
"colnames(wCoefs) <- c(0, scales);print('D');"
"png('toto2.png'); image(wCoefs); print('E');"
);
return 0;
} |
Partager