Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making htmlwidgets in Rmarkdown mobile friendly

The default options of rmarkdown is to set the fig.width = 12. I would like it to auto-adujust for the width of a mobile device if this is possible.

I'm hosting the following Rmarkdown at http://akdata.org/misc/leafletmobile

---
title: "Untitled"
output: html_document

---


```{r}      
  library(leaflet)
  leaflet() %>% addTiles()
``

It will adjust to the width of the screen when I play with different mobile devices in chrome devtools though. I have a physical Samsung Galaxy 5 that I'm testing with.

like image 447
cylondude Avatar asked Mar 10 '16 00:03

cylondude


2 Answers

I think specifying a percentage width will give you the result you want. Below is how you would do it in rmarkdown and here is a live example. Unfortunately, if you specify a percentage height also, your screen will appear blank due to some bug somewhere, so it is not completely responsive, but it still adjusts nicely on my iPhone.

---
title: "responsive_leaflet"
author: "TimelyPortfolio"
date: "March 24, 2016"
output:
  html_document:
   mathjax: null
---

```{r echo=FALSE, warning=FALSE}
# no help from a framework
#  just percentage height and width
library(leaflet)

l <- leaflet(width="100%") %>%
  addTiles()
l
```

```{r echo=FALSE, warning=FALSE}
# demonstrate with Bootstrap
library(shiny)

fluidRow(
  column(width=10,l)
)
```
like image 69
timelyportfolio Avatar answered Oct 06 '22 19:10

timelyportfolio


<div id="htmlwidget-4092" style="width:75%; height:75%; position:absolute" class="leaflet html-widget"></div>

This block of code appears near the bottom of the source. The dimensions can be made to percentages, and it will auto-resize to fit the screen. Any percentages should work. Also, position: absolute is required to ensure that it stays in the correct location.

Note: I would recommend finding a way to shorten those massive links in the page source.

like image 45
StardustGogeta Avatar answered Oct 06 '22 19:10

StardustGogeta