使用RInside和Rcpp保存晶格图 [英] Saving Lattice Plots with RInside and Rcpp

查看:103
本文介绍了使用RInside和Rcpp保存晶格图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用RInside在C ++中构建R应用程序.我想使用代码将绘图另存为指定目录中的图像,

I am trying to build an R application in C++ using RInside. I wanted to save the plots as images in specified directory using codes,

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()

如果直接从R运行,它将在指定目录中创建一个png文件.但是使用RInside的C ++调用,我无法重现相同的结果. (我可以使用C ++调用来重现所有基本图.只有Lattice和ggplots出现问题)

It creates a png file in the specified directory if directly run from R. But using C++ calls from RInside, I was not able to reproduce the same result. (I could reproduce all base plots using C++ calls. Problem with only Lattice and 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()

如果我在R中运行上述代码而没有任何问题,则会创建

png文件.但是从C ++调用中,将创建一个带有标题和x-y标签的空白面板的png文件,而不是完整的图.

png file is getting created if I run the above code in R without any problem. But from C++ calls, a png file with empty panel with title and x-y label is getting created and not a complete plot.

我正在使用R.parseEval()函数对R进行C ++调用.

I'm using the function R.parseEval() for C++ call to R.

如何正确获取正确的晶格和ggplot2图?

How to get proper lattice and ggplot2 plots properly?

推荐答案

以下内容将格子xyplot打印到png.这是一个最小的示例,作为变体完成 在rinside_sample11.cpp周围.

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);
}

它将为我创建此文件:

这篇关于使用RInside和Rcpp保存晶格图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆