VS中使用QT调用R脚本
一开始想直接把R编译成库然后调用R,后来查了n多资料,发现VS中是无法办到的,官方也给出了一句话,大概意思就是没可能在VS中使用R提供的C++接口,大概是涉及到了底层的ABI的原因,具体也不太清楚。
于是就想到了直接采用新建文件然后写成.R脚本的方式来调用R,这种使用方式必须安装R,然后从程序内部调用RScript.exe以及相应脚本。
QFile Rfile("C:\\temp\\RScript\\heatmap.R"); Rfile.open(QIODevice::WriteOnly); QTextStream RfileWrite(&Rfile); RfileWrite << "library(RColorBrewer)" << endl; RfileWrite << "library(gplots)" << endl; RfileWrite << "x = read.table(\"C:\\\\temp\\\\RData\\\\heatmap.dat\", header = TRUE, sep=\"\t\")" << endl; RfileWrite << "mat = data.matrix(x)" << endl; RfileWrite << "png(file = \"C:\\\\temp\\\\RPic\\\\heatmap.png\", bg = \"transparent\")" << endl; RfileWrite << "heatmap.2(mat, Rowv = TRUE, Colv = TRUE, distfun = dist,hclustfun = hclust,\ xlab = \"X data\", ylab = \"Y data\",\ key = TRUE,\ keysize = 2,\ trace = \"none\",\ density.info = c(\"none\"),\ margins = c(5, 5),\ col = brewer.pal(10, \"PiYG\")\ )"<<endl; RfileWrite << "dev.off()" << endl; Rfile.flush(); Rfile.close(); //system("C:\\\"program files\"\\R\\R-3.1.2\\bin\\RScript D:\\drawtest.R"); //system("RScript D:\\drawtest.R"); ShellExecuteA(NULL, "open", "RScript.exe", "C:\\temp\\RScript\\heatmap.R", NULL, SW_HIDE);