在knitr / rmarkdown中作为png plotly

问题描述 投票:12回答:4

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

图表的快照如下所示:

enter image description here

根据plotly help page

如果您使用带有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.
r 3d knitr r-markdown plotly
4个回答
6
投票

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

install.packages("webshot")

此外,您需要通过安装phantomjs

webshot::install_phantomjs()

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

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


3
投票

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


0
投票

R markdown compile error:相同的问题。你需要选择你想要编织的格式,试着看看我的。

---
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
---

KNIT


0
投票

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

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

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

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

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

在示例中,base_plot是情节的名称。

© www.soinside.com 2019 - 2024. All rights reserved.