在knitr/rmarkdown中作为png plotly

Meg*_*ron 13 3d r knitr r-markdown plotly

以下Rmarkdown以HTML格式呈现3D图形,但不是PDF格式.

Testing plotly

```{r}
library(plotly)
p <- plot_ly(data=iris, x=~Sepal.Length, y=~Sepal.Width, z=~Petal.Length, 
             color=~Species, symbols=c(0,1), type="scatter3d", mode="markers")
p
```
Run Code Online (Sandbox Code Playgroud)

图表的快照如下所示:

在此输入图像描述

根据情节帮助页面:

如果您使用带有HTML输出的rmarkdown,则在代码块中打印绘图对象将生成交互式HTML图形.当使用带有非HTML输出的rmarkdown时,打印一个绘图对象将导致图形的png屏幕截图.

有没有办法在PDF中呈现图形图?

注意:错误来自rmarkdown::render():

Error: Functions that produce HTML output found in document targeting latex output.
Please change the output type of this document to HTML. Alternatively, you can allow
HTML output in non-HTML formats by adding this option to the YAML front-matter of
your rmarkdown file:

  always_allow_html: yes

Note however that the HTML output will not be visible in non-HTML formats.
Run Code Online (Sandbox Code Playgroud)

J_F*_*J_F 6

我创建了一个小的解决方法,它将本地图形图像保存为png文件并将其导回RMD文件.您需要包webshot,您可以通过以下方式加载:

install.packages("webshot")
Run Code Online (Sandbox Code Playgroud)

此外,您需要通过安装phantomjs

webshot::install_phantomjs()
Run Code Online (Sandbox Code Playgroud)

然后(当phantomjs在你的PATH中时),你可以创建你的RMD文件:

---
title: "Untitled"
output: pdf_document
---

```{r}
library(plotly)
p <- plot_ly(economics, x = ~date, y = ~unemploy / pop)

tmpFile <- tempfile(fileext = ".png")
export(p, file = tmpFile)
```
![Caption for the picture.](`r tmpFile`)
Run Code Online (Sandbox Code Playgroud)

这对我有用..也许这对你来说也是一种解决方法!

  • Phantom的WebGL [不支持](http://phantomjs.org/supported-web-standards.html).您需要使用firefox或chrome进行渲染(IIRC`webshot`不支持使用其他驱动程序). (3认同)