Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center leaflet in a rmarkdown document

I want to create an html document with a centered leaflet widget inside it.

---
title: "Exemple"
author: "Antoine Bichat"
date: "31/08/2018"
output: html_document
---

```{r leaflet, fig.align="center"}
library(leaflet)

leaflet() %>%
  addTiles() %>% 
  addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")
```

The first idea with fig.align="center" doesn't work.

I found this question and I adapted it to my case:

<style>
/* resize the widget container */
.leaflet { 
  width: 80% !important;
}

/* center the widget */
div.leaflet-control-container {
  margin: auto !important;
}
</style>

but if I don't want to have an out.width of 100% (and I don't want to), it doesn't work.

Or maybe I did it wrong... Actually I prefer a 100% R/Rmarkdown solution but I'm open to everything :)

like image 581
abichat Avatar asked Aug 31 '18 09:08

abichat


People also ask

How do you center something in R markdown?

Centering Images You can use the knitr include_graphics() function along with the fig. align='center' chunk option. This technique has the benefit of working for both HTML and LaTeX output. You can add CSS styles that center the image (note that this technique works only for HTML output).

How do I center a header in markdown?

TLDR. To center images, text, and anything else in Github markdown and READMEs simply wrap the element in an HTML tag with the align attribute set to "center" .


1 Answers

Try the CSS code margin: auto for .html-widget

---
title: "Exemple"
author: "Antoine Bichat"
date: "31/08/2018"
output: html_document
---

<style>
.html-widget {
    margin: auto;
}
</style>

```{r leaflet, fig.align="center"}
library(leaflet)

leaflet() %>%
  addTiles() %>% 
  addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")
```

enter image description here

That will center every htmlwidget. You can also put :

<style>
.leaflet {
    margin: auto;
}
</style>

For centering only the leaflet.

like image 164
Colin FAY Avatar answered Oct 02 '22 04:10

Colin FAY