简体   繁体   English

如何基于R中的屏幕外(位图)图形创建“交互式”图形设备?

[英]How do I create an “interactive” graphics device based on off-screen (bitmap) graphics in R?

Plotting large plots in R can be painfully slow. 在R中绘制大块地块可能会非常缓慢。 I'm trying some workarounds but even with the maximally-buffered "dbcairo" X11 device, plotting seems to take much longer than it could. 我正在尝试一些解决方法,但即使使用最大缓冲的“dbcairo”X11设备,绘图似乎也需要更长的时间。 I noticed that the "png" device is faster for both small plots and large plots. 我注意到“png”设备对于小块地块和大块地块都更快。

Small plots, png() beats X11() by about 3x: 小块地块,png()比X11()大约3倍:

> system.time({X11(type="dbcairo"); plot(1:1e3); dev.off()})
   user  system elapsed 
  0.234   0.029   0.373 
> system.time({png("file.png",type="cairo"); plot(1:1e3); dev.off()})
   user  system elapsed 
  0.114   0.000   0.113 
> system.time({png("file.png",type="Xlib"); plot(1:1e3); dev.off()})
   user  system elapsed 
  0.056   0.000   0.107 

Large plots, png() beats X11() by 2.3x to 46x: 大块地块,png()击败X11()2.3倍至46倍:

> system.time({X11(type="dbcairo"); plot(1:1e5); dev.off()})
   user  system elapsed 
 14.420   0.157  15.491 
> system.time({png("file.png",type="cairo"); plot(1:1e5); dev.off()})
   user  system elapsed 
  6.790   0.001   6.826 
> system.time({png("file.png",type="Xlib"); plot(1:1e5); dev.off()})
   user  system elapsed 
  0.144   0.010   0.340 

The results were pretty much the same with other X11() device types (being similar to "dbcairo") and other bitmap image types (being similar to "png"). 结果与其他X11()设备类型(类似于“dbcairo”)和其他位图图像类型(类似于“png”)几乎相同。 I'm guessing png() type "cairo" takes longer than "Xlib" because it produces antialiased output. 我猜png()类型“cairo”需要比“Xlib”更长的时间,因为它产生抗锯齿输出。

With a medium-sized ggplot2 plot, I found that png() is 1.7x to 2.6x faster than X11() (that's using png(..); plot(g); dev.off() , not ggsave() ) 使用中等大小的ggplot2图,我发现png()比X11()快1.7到2.6倍(使用png(..); plot(g); dev.off() ,而不是ggsave()

Since I can load and reload a PNG file almost instantaneously using a minimalist image-viewer like "feh", I'm wondering why I don't use png() as my primary plotting device with R. 由于我可以使用像“feh”这样的极简主义图像查看器几乎立即加载和重新加载PNG文件,我想知道为什么我不使用png()作为我的主要绘图设备。

The problem of course is that the PNG file isn't written to disk until I call dev.off(). 问题当然是在调用dev.off()之前,PNG文件没有写入磁盘。 This crimps the style of the standard plotting interface, where I set the device once and then bring up various plots, sometimes adding lines or text, while viewing each change immediately in the plot window. 这会压缩标准绘图界面的风格,我将设备设置一次,然后调出各种绘图,有时添加线条或文本,同时立即在绘图窗口中查看每个更改。

Would it be difficult to create a new R graphics device which writes image files to PNG (or some other image format), but uses a simple image viewer like "feh" to display them after each plotting command? 是否很难创建一个将图像文件写入PNG(或其他图像格式)的新R图形设备,但使用像“feh”这样的简单图像查看器在每个绘图命令后显示它们? In other words, I'm looking for an "interactive" plotting device like X11, but which uses the bitmap off-screen rendering facilities of existing devices such as png(), jpeg(), or tiff() to draw the image. 换句话说,我正在寻找像X11这样的“交互式”绘图设备,但它使用现有设备(如png(),jpeg()或tiff()的位图离屏渲染工具来绘制图像。 Or maybe the png() device can be modified to have an option which gives it this behavior. 或者也许可以修改png()设备以使其具有这种行为的选项。 (Or maybe I should be using knitR for everything ... but I am more familiar with the command line ...) (或者也许我应该将knitR用于所有内容......但我对命令行更熟悉......)

If X11 drives you up the wall and you don't want to use the RStudio plot viewer (or can't because you're on a remote server where you can't install RStudio Server), one interesting alternative is to use the rmote package. 如果X11驱动你的墙,你不想使用RStudio情节查看器(或者因为你在一个无法安装RStudio Server的远程服务器上而不能),一个有趣的选择是使用rmote包。 You can install it on either your own local R installation or, if you're SSHing into a remote machine and are willing to forward the port, on a remote R instance. 您可以在自己的本地R安装上安装它,或者,如果您正在SSH连接到远程计算机并且愿意转发端口,则可以在远程R实例上安装它。 Then any plots and help files you print will be redirected to a local web server, where you can access them from your browser. 然后,您打印的任何绘图和帮助文件都将重定向到本地Web服务器,您可以从浏览器访问它们。

It also comes with some nice extras, like: 它还附带了一些不错的附加功能,例如:

  • A plot history, 情节历史,
  • The ability to right-click and save plots, as you would any other image in a browser, and 能够右键单击并保存绘图,就像浏览器中的任何其他图像一样
  • The ability to choose which kinds of output are sent to the server. 能够选择将哪种输出发送到服务器。

I've found this a nice alternative to X11 forwarding with plots on a remote server, and it works well locally too 🙂 我发现这是一个很好的替代X11转发与远程服务器上的情节,它也适用于本地🙂

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM