如何在RMarkdown文档(PDF,HTML)中排列HTML小部件 [英] How to arrange HTML Widgets inside of a RMarkdown Document (PDF, HTML)

查看:464
本文介绍了如何在RMarkdown文档(PDF,HTML)中排列HTML小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm working in a R notebook and would like use it to create two ouptuts: an HTML document and PDF document.

我的分析包括传单地图(html窗口小部件),当我将笔记本编织成PDF文档时,这会引起问题.感谢knitr软件包中现在包含的 webshot 函数,"knitr会尝试生成静态使用webshot软件包自动生成HTML小部件的屏幕截图"( https://github.com/yihui/knitr/blob/master/NEWS.md ).

My analysis includes leaflet maps (html widgets), which is causing problems when I knit the notebook to a PDF document. Thanks to the webshot function now included in the knitr package, "knitr will try to generate static screenshots for HTML widgets automatically using the webshot package" (https://github.com/yihui/knitr/blob/master/NEWS.md).

当我的输出是一系列彼此重叠的传单地图时,此方法很好用,但我想将地图组合在一起,以更简洁的行排列方式(请参见下图).

This works fine when my output is a series of leaflet maps stacked on top of each other, but I would like to group the maps together in a more concise row arrangement (see image below).

以下是我的R笔记本的可复制示例:要点

Here's a reproducible example of my R notebook: gist

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

Unfortunately, when I try to knit this to a PDF document I get the following error message:

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.

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

How can I get this single-row arrangement in a PDF document?

推荐答案

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

If I understand you correctly, then all you have to do is to add some chunk options. The key here is the option fig.show='hold' which determines that all plots in the chunk will be collected and displayed together at the very end of the chunk.

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

如果要同时具有pdf和html输出的这种格式,则可以将此脚本添加到Rmd文档的正文中(不在大块内):

If you want to have this format for both pdf and html output you could add this script to the body ofyour Rmd document (not inside a chunk):

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

尝试通过块选项out.extra添加此CSS代码段不起作用,因为LaTeX不知道如何处理CSS.尽管在编译为pdf时会忽略JS代码.

Trying to add this CSS snippet via the chunk option out.extra does not work, since LaTeX does not know how to deal with CSS. Though the JS code is ignored when compiled to pdf.

这篇关于如何在RMarkdown文档(PDF,HTML)中排列HTML小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆