微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我在 R notebook工作,并希望用它来创建两个ouptuts:HTML文档和PDF文档.

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

当我的输出是一系列相互叠加的传单地图时,这种方法很好,但我想将这些地图组合在一起,形式更简洁(见下图).

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

不幸的是,当我尝试将其编织为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.

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

解决方法

如果我理解正确,那么你所要做的就是添加一些块选项.这里的关键是选项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
```

如果你想为pdf和html输出都有这种格式,你可以将这个脚本添加到你的Rmd文档的主体中(不在块内):

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐