Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to arrange HTML Widgets inside of a RMarkdown Document (PDF, HTML)

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

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

screenshot

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

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.

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

like image 544
Tiernan Avatar asked Sep 18 '16 21:09

Tiernan


People also ask

How do I display a HTML Widget in R Markdown?

If we are using htmlwidgets we must set the option self_contained: in the YAML header to false We then use the frameWidget () function from the widgetframe package on this object This automatically frames our widget and displays it in our R Markdown document

Why can’t I use R Markdown with word or PDF or PowerPoint?

Word, PDF and PowerPoint are non-HTML forms of media, and so we cannot simply call upon our HTML widgets or frame them like we do with other knit-able R Markdown files. In Word and PDF outputs, the ‘offline’ nature of these documents means we cannot have interactive widgets In this example we will display a screenshot in this html document.

How to publish Rmarkdown document on your website using Hugo?

So, to publish the RMarkdown document on our website, we can generate a markdown file then give it to Hugo’s markdown renderer or convert it directly to HTML. In order to use these widgets, we are going to generate an HTML file directly from the r markdown. The HTML page will include some javascript libraries.

Why can’t I use HTML widgets with word and PDF outputs?

Word, PDF and PowerPoint are non-HTML forms of media, and so we cannot simply call upon our HTML widgets or frame them like we do with other knit-able R Markdown files. In Word and PDF outputs, the ‘offline’ nature of these documents means we cannot have interactive widgets


1 Answers

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

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>

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.

enter image description here

like image 135
Martin Schmelzer Avatar answered Oct 25 '22 13:10

Martin Schmelzer