Saving Lattice Plots with RInside and Rcpp

萝らか妹 提交于 2019-12-06 15:04:51

The following prints a lattice xyplot to a png. It is a minimal example, done as a variation around 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);
}

It creates this file for me:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!