Man*_*j G 1 plot r lattice rcpp rinside
我正在尝试使用RInside在C++中构建一个R应用程序.我想使用代码将图表保存为指定目录中的图像,
png(filename = "filename", width = 600, height = 400)
xyplot(data ~ year | segment, data = dataset, layout = c(1,3),
type = c("l", "p"), ylab = "Y Label", xlab = "X Label",
main = "Title of the Plot")
dev.off()
Run Code Online (Sandbox Code Playgroud)
png如果直接从R运行,它会在指定目录中创建一个文件.但是使用来自RInside的C++调用,我无法重现相同的结果.(我可以使用C++调用重现所有基础图.仅使用Lattice和ggplots的问题)
我也使用以下代码,
myplot <- xyplot(data ~ year | segment, data = dataset, layout = c(1,3),
type = c("l", "p"), ylab = "Y Label", xlab = "X Label",
main = "Title of the Plot")
trellis.device(device = "png", filename = "filename")
print(myplot)
dev.off()
Run Code Online (Sandbox Code Playgroud)
png如果我在R中运行上面的代码没有任何问题,则会创建文件.但是从C++调用开始,创建了一个png带有title和xy标签的空面板的文件,而不是一个完整的图.
我正在使用R.parseEval()C++调用函数的函数.
如何正确获得正确的格子和ggplot2图?
以下将格子打印xyplot到png.这是一个最小的例子,作为变体来完成rinside_sample11.cpp.
#include <RInside.h> // for the embedded R via RInside
#include <unistd.h>
int main(int argc, char *argv[]) {
// create an embedded R instance
RInside R(argc, argv);
// evaluate an R expression with curve()
// because RInside defaults to interactive=false we use a file
std::string cmd = "library(lattice); "
"tmpf <- tempfile('xyplot', fileext='.png'); "
"png(tmpf); "
"print(xyplot(Girth ~ Height | equal.count(Volume), data=trees)); "
"dev.off();"
"tmpf";
// by running parseEval, we get the last assignment back, here the filename
std::string tmpfile = R.parseEval(cmd);
std::cout << "Can now use plot in " << tmpfile << std::endl;
exit(0);
}
Run Code Online (Sandbox Code Playgroud)
它为我创建了这个文件:
