如何在RMarkdown文档中安排HTML小部件(PDF,HTML)

Tie*_*nan 6 r knitr leaflet r-markdown htmlwidgets

我正在使用R笔记本,并希望用它来创建两个ouptuts:HTML文档和PDF文档.

我的分析包括小册子地图(html小部件),当我将笔记本编织成PDF文档时导致问题.由于包中webshot现在包含的功能knitr,"knitr将尝试使用webshot包自动生成HTML小部件的静态屏幕截图"(https://github.com/yihui/knitr/blob/master/NEWS.md).

当我的输出是一系列相互叠加的传单地图时,这很好用,但我想将这些地图组合成一个更简洁的行排列(见下图).

截图

这是我的R笔记本的可重现的例子:要点

不幸的是,当我尝试将其编织为PDF文档时,我收到以下错误消息:

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)

如何在PDF文档中获得此单行排列?

Mar*_*zer 5

如果我理解正确的话,那么您所要做的就是添加一些块选项。这里的关键是fig.show='hold'确定块中的所有绘图将被收集并在块的最后一起显示的选项。

---
title: "R Notebook"
output:
  pdf_document: 
    keep_tex: yes
  html_notebook: default
---

###Default Arrangement
```{r, echo=FALSE,message=FALSE, fig.height=4, fig.width=2, fig.show='hold'}
#devtools::install_github("wch/webshot")

library(leaflet)
library(htmltools)
library(RColorBrewer)

m1 <- leaflet(quakes) %>% 
        addTiles() %>% 
        addMarkers(lng=174.768, lat=-36.852)

m2 <- leaflet(quakes) %>% 
        addProviderTiles("Esri.WorldGrayCanvas") %>% 
        addMarkers(lng=174.768, lat=-36.852)

m3 <- leaflet(quakes) %>%
        addProviderTiles("Stamen.Toner") %>%
        addMarkers(lng=174.768, lat=-36.852)
m1
m2
m3
```
Run Code Online (Sandbox Code Playgroud)

如果您希望 pdf 和 html 输出都采用这种格式,您可以将此脚本添加到 Rmd 文档的正文中(而不是在块内):

<script>
  $(document).ready(function() {
    $('.leaflet').css('float','left');
  });
</script>
Run Code Online (Sandbox Code Playgroud)

尝试通过 chunk 选项添加此 CSS 片段out.extra不起作用,因为 LaTeX 不知道如何处理 CSS。虽然编译为pdf时JS代码被忽略。

在此输入图像描述