【问题标题】:Save a ggplot2 object as ggplotly and save to disk in Linux将 ggplot2 对象另存为 ggplotly 并在 Linux 中保存到磁盘
【发布时间】:2017-02-22 01:32:00
【问题描述】:

尝试在R 中使用plotly 迈出我的第一步。

我想将我的ggplot2 对象转换为ggplotly 对象,然后在Linux 平台上将其另存为html。我希望能够从命令行调用此 R 代码并将其作为脚本执行,而不是通过 RStudio 运行。

我认为这样做可以(from plotly's manual):

require(ggplot2)
require(plotly)

ggiris <- qplot(Petal.Width, Sepal.Length, data = iris, color = Species)
ggiris.ly <- ggplotly(ggiris)
htmlwidgets::saveWidget(ggiris.ly,"ggiris.html")

但是ggplotly(ggiris) 抛出这个错误:

Error in .External2(C_X11, paste("png::", filename, sep = ""), g$width,  :
  unable to start device PNG
In addition: Warning message:
In dev_fun(tmpPlotFile, width = deviceWidth, height = deviceHeight) :
  unable to open connection to X11 display ''

然后我安装了XQuartz 以便能够将ssh -X -Y 从我的Mac 连接到我的linux 系统。

ggiris.ly <- ggplotly(ggiris)

打开R Graphics 设备,然后

htmlwidgets::saveWidget(ggiris.ly,"~/Downloads/ggiris.html")

抛出此错误:

Error in htmlwidgets::saveWidget(ggiris.ly, "~/Downloads/ggiris.html") : 
  Saving a widget with selfcontained = TRUE requires pandoc. For details see:
https://github.com/rstudio/rmarkdown/blob/master/PANDOC.md

有什么想法吗?

顺便说一句, 我正在使用plotly_4.5.6ggplot2_2.2.1

【问题讨论】:

  • 谢谢。我注意到并修复了它,但错误是无关的。
  • 已修复。再次感谢。
  • 在 win7 上与相同版本工作正常
  • 在 Mac 上也能正常工作。问题是 Linux 特有的
  • 不是解决方法,但可以解决...创建一个 rMarkdown 对象,其中只有绘图...您将拥有一个交互式 html 文档

标签: r linux ggplot2 plotly


【解决方案1】:

您需要在系统上安装 pandoc。如果您没有 root 访问权限,请运行 sudo apt-get install pandoc 或让您的系统管理员这样做。

或者,您可以使用htmlwidgets::saveWidget(h, "test.html", selfcontained=FALSE) 保存

【讨论】:

  • 已安装 pandoc
【解决方案2】:

我确保当 ssh -X 从我的 Mac 到我的远程服务器时,我没有打开 firewall

然后上面的代码在没有警告的情况下执行。 (期望通过打开一个空的图形设备来创建一个空白的Rplot.pdf 文件)。

【讨论】:

    猜你喜欢
    • 2011-04-09
    • 1970-01-01
    • 2014-05-30
    • 2016-10-31
    • 1970-01-01
    • 2012-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多