简体   繁体   English

如何将R中的dygraph包生成的图导出到png?

[英]How to export graphs generated with the dygraphs package in R to png?

How can I export a plot generated with the R package "dygraphs" to a png file? 如何将使用R包“ dygraphs”生成的图导出到png文件?

library(dygraphs)
sample_dat <- data.frame(1:1000, rnorm(1000))
dygraph(sample_dat)

As hrbrmstr suggested in the comments, try using webshot . 正如hrbrmstr在评论中建议的那样,尝试使用webshot

library(dygraphs)
sample_dat <- data.frame(1:1000, rnorm(1000))
dygraph(sample_dat)

This produces the following output in my default web browser (safari in this instance): 这会在我的默认Web浏览器(在这种情况下为野生动物园)中产生以下输出:

在此处输入图片说明

You will notice in the address bar a path to where the file is temporarily stored on your local drive: 您会在地址栏中注意到文件临时存储在本地驱动器上的路径:

在此处输入图片说明

Copy paste that path like in the following: 复制粘贴该路径,如下所示:

webshot("file:///var/folders/47/71_55p097y94jptwk6t75f8w0000gn/T/RtmpBPhijO/viewhtml2f836e308321/index.html", 
    "dygraph.png")

Also note, you may get the following warning if you don't have PhantomJS installed: 另请注意,如果未安装PhantomJS,则可能会收到以下警告:

PhantomJS not found. 找不到PhantomJS。 You can install it with webshot::install_phantomjs(). 您可以使用webshot :: install_phantomjs()安装它。 If it is installed, please make sure the phantomjs executable can be found via the PATH variable. 如果已安装,请确保可以通过PATH变量找到phantomjs可执行文件。

...in which case run webshot::install_phantomjs() ...在这种情况下,请运行webshot::install_phantomjs()

Once, webshot has saved it, you will notice a new file saved to your working directory: 一旦webshot将其保存,您将注意到一个新文件已保存到您的工作目录中:

在此处输入图片说明

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

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