在knitr / rmarkdown中作为png plotly

时间:2022-10-21 06:12:44

The following Rmarkdown renders the plotly 3D graph in HTML, but not in PDF.

以下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
```

A snapshot of the graph appears as follows:

该图的快照如下所示:

在knitr / rmarkdown中作为png plotly

According to the plotly help page:

根据情节帮助页面:

If you are using rmarkdown with HTML output, printing a plotly object in a code chunk will result in an interactive HTML graph. When using rmarkdown with non-HTML output, printing a plotly object will result in a png screenshot of the graph.

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

Is there a way to render the plotly graph in a PDF?

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

Note: The error from rmarkdown::render() is:

注意: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.

4 个解决方案

#1


5  

I have created a little workaround, which saves the plotly images locally as png-file and imports it back to the RMD file. You need the package webshot, which you can load via:

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

install.packages("webshot")

Further more, you need to install phantomjs via

此外,您需要通过安装phantomjs

webshot::install_phantomjs()

Then (when phantomjs is in your PATH), you can create your RMD file:

然后(当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`)

This works for me .. perhaps it's a workaround for you, too!

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

#2


3  

As @hrbrmstr commented, export() previously didn't support WebGL at all, but more recent versions support exporting to png via RSelenium (see help(export, package = "plotly")). If you need pdf export, you'll have to pay for a cloud account -- https://plot.ly/products/cloud/

正如@hrbrmstr评论的那样,export()以前根本不支持WebGL,但是更新的版本支持通过RSelenium导出到png(参见help(export,package =“plotly”))。如果您需要pdf导出,则必须支付云帐户 - https://plot.ly/products/cloud/

#3


0  

Same issue with R markdown compile error:. You need to choose what format you want to KNIT to, tried to look at mine.

与R markdown编译错误相同的问题:您需要选择要编织的格式,试图查看我的格式。

---
title: "<img src='www/binary-logo.jpg' width='240'>"
subtitle: "[<span style='color:blue'>binary.com</span>](https://github.com/englianhu/binary.com-interview-question) Interview Question I"
author: "[<span style='color:blue'>®γσ, Lian Hu</span>](https://englianhu.github.io/) <img src='www/ENG.jpg' width='24'> <img src='www/RYO.jpg' width='24'>白戸則道®"
date: "`r Sys.Date()`"
output:
  tufte::tufte_html:
    toc: yes
  tufte::tufte_handout:
    citation_package: natbib
    latex_engine: xelatex
  tufte::tufte_book:
    citation_package: natbib
    latex_engine: xelatex
link-citations: yes
---

在knitr / rmarkdown中作为png plotly

#4


0  

What I do so that rendering to PDF's work but you can still have the interactive plots in other knit types and in rmarkdown files in r studio is:

我这样做是为了渲染PDF的工作,但你仍然可以在其他编织类型和r studio中的rmarkdown文件中使用交互式图:

this goes in the setup block (or really, anywhere early in the file):

这是在设置块中(或者实际上,在文件的早期):

is_pdf <- try (("pdf_document" %in% rmarkdown::all_output_formats(knitr::current_input())), silent=TRUE)
is_pdf <- (is_pdf == TRUE)

then this is used to render any plotly plot based on what kind of document you are creating:

然后,这用于根据您创建的文档类型呈现任何绘图:

if (is_pdf) { export(base_plot) } else {base_plot}

in the example base_plot is the name of the plot.

在示例中,base_plot是绘图的名称。

#1


5  

I have created a little workaround, which saves the plotly images locally as png-file and imports it back to the RMD file. You need the package webshot, which you can load via:

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

install.packages("webshot")

Further more, you need to install phantomjs via

此外,您需要通过安装phantomjs

webshot::install_phantomjs()

Then (when phantomjs is in your PATH), you can create your RMD file:

然后(当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`)

This works for me .. perhaps it's a workaround for you, too!

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

#2


3  

As @hrbrmstr commented, export() previously didn't support WebGL at all, but more recent versions support exporting to png via RSelenium (see help(export, package = "plotly")). If you need pdf export, you'll have to pay for a cloud account -- https://plot.ly/products/cloud/

正如@hrbrmstr评论的那样,export()以前根本不支持WebGL,但是更新的版本支持通过RSelenium导出到png(参见help(export,package =“plotly”))。如果您需要pdf导出,则必须支付云帐户 - https://plot.ly/products/cloud/

#3


0  

Same issue with R markdown compile error:. You need to choose what format you want to KNIT to, tried to look at mine.

与R markdown编译错误相同的问题:您需要选择要编织的格式,试图查看我的格式。

---
title: "<img src='www/binary-logo.jpg' width='240'>"
subtitle: "[<span style='color:blue'>binary.com</span>](https://github.com/englianhu/binary.com-interview-question) Interview Question I"
author: "[<span style='color:blue'>®γσ, Lian Hu</span>](https://englianhu.github.io/) <img src='www/ENG.jpg' width='24'> <img src='www/RYO.jpg' width='24'>白戸則道®"
date: "`r Sys.Date()`"
output:
  tufte::tufte_html:
    toc: yes
  tufte::tufte_handout:
    citation_package: natbib
    latex_engine: xelatex
  tufte::tufte_book:
    citation_package: natbib
    latex_engine: xelatex
link-citations: yes
---

在knitr / rmarkdown中作为png plotly

#4


0  

What I do so that rendering to PDF's work but you can still have the interactive plots in other knit types and in rmarkdown files in r studio is:

我这样做是为了渲染PDF的工作,但你仍然可以在其他编织类型和r studio中的rmarkdown文件中使用交互式图:

this goes in the setup block (or really, anywhere early in the file):

这是在设置块中(或者实际上,在文件的早期):

is_pdf <- try (("pdf_document" %in% rmarkdown::all_output_formats(knitr::current_input())), silent=TRUE)
is_pdf <- (is_pdf == TRUE)

then this is used to render any plotly plot based on what kind of document you are creating:

然后,这用于根据您创建的文档类型呈现任何绘图:

if (is_pdf) { export(base_plot) } else {base_plot}

in the example base_plot is the name of the plot.

在示例中,base_plot是绘图的名称。