【问题标题】:Saving Lattice Plots with RInside and Rcpp使用 RInside 和 Rcpp 保存格点图
【发布时间】:2014-06-24 04:16:07
【问题描述】:

我正在尝试使用 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()

如果直接从 R 运行,它会在指定目录中创建一个 png 文件。但是使用来自 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()

png 如果我在 R 中运行上述代码没有任何问题,则会创建文件。但是从 C++ 调用中,一个带有标题和 x-y 标签的空面板的 png 文件正在创建,而不是完整的绘图。

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

如何正确地得到合适的 lattice 和 ggplot2 图?

【问题讨论】:

  • 也许您需要一个虚拟 x11 字体设备,请参阅有关xvfb 和/或[r] xvfb 的问题。我只用这种方式创建过基础图。
  • 而且,如果可以的话,还用推文打我有点不礼貌。我不欠你的支持,只要我有时间,我就做,不麻烦。所以请不要把它变成一个。
  • @DirkEddelbuettel:对不起。我知道我一定会得到你的回复。所以,我这样做的目的是为了更快地得到它。后来我意识到这不是什么好行为,我不应该这样做。以后不会再重复了。非常感谢你的回复。 :)

标签: r plot lattice rcpp rinside


【解决方案1】:

以下将格子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);
}

它为我创建了这个文件:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-05
    • 2013-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-30
    • 2018-01-10
    • 1970-01-01
    相关资源
    最近更新 更多