timlxq
timlxq

Reputation: 41

Auto sizing Leaflet Widget in Rmd HTML Document

I am trying to host a leaflet map on Rpubs.com, but am facing problems trying to size the map size in the knitted Rmd file before publishing. Ideally, I would like for the map to resize based on the browser window dimensions. There are a couple of examples on Rpubs.com that achieve this, such as this one, but I couldn't find any useful solutions to scale my map.

This is the map as it currently stands. Note the white space at the sides and hard-coded length.

The code chunk I used to generate the Leaflet object is as follows


    ```{r echo = FALSE, fig.height= 10, fig.width = 12, fig.align="left"}
    mata_cams <- leaflet() %>% 
      setView(lat = 1.352754, lng = 103.866692, zoom = 12) %>%
      addTiles(urlTemplate = "http://mt0.google.com/vt/lyrs=m&hl=en&x={x}&y={y}&z={z}&s=Ga", 
               attribution = 
               '<a href = "maps.google.com">Google Maps</a>
               <a href = "https://www.google.com/intl/en-GB_US/help/terms_maps/">(Terms of Service)</a>', 
               group = "Google Maps") %>%
      addProviderTiles(provider = "OneMapSG.Original", group = "OneMapSG") %>% 
      addProviderTiles(provider = "OneMapSG.Grey", group = "OneMapSG (Grey)") %>% 
      setMaxBounds(lat1 = 1.157170,
                   lat2 = 1.476471,
                   lng1 = 103.588712,
                   lng2 = 104.1) %>%
      addCircleMarkers(~location_longitude,
                       ~location_latitude,
                       color = "#f07b0e",
                       radius = 5,
                       group = "Speed Cameras",
                       popup = popup_speed,
                       fillOpacity = 0.5,
                       stroke = FALSE,
                       data = all_speed_cams) %>%
      addCircleMarkers(data = red_light_cams@data,
                       lat = ~LATITUDE,
                       lng = ~LONGITUDE,
                       color = "red",
                       group = "Red Light Cameras",
                       popup = popup_rl,
                       radius = 5,
                       fillOpacity = 0.5,
                       stroke = FALSE) %>%
      addKML(kml_road,
             markerType = "circleMarker",
             fillColor = "blue",
             fillOpacity = 0.5,
             stroke = FALSE,
             markerOptions = leaflet::markerOptions(radius = 5),
             group = "Road Cameras") %>%
      addLegend(colors = c("#f07b0e", "red", "blue"), 
                labels = c("Speed Cameras", "Red Light Cameras", "Road Cameras"),
                values = NULL,
                position = "topright",
                title = "Camera Type") %>%
      addLayersControl(baseGroups = c("Google Maps", "OneMapSG", "OneMapSG (Grey)"),
                       overlayGroups = c("Speed Cameras", "Red Light Cameras", "Road Cameras"),
                       options = layersControlOptions(collapsed = FALSE))

    mata_cams
    ```

All other code chunks in the Rmd use include = FALSE, and the YAML header only includes the line output: html_document.

I have tried

The solution I finally settled on was to use fig.height and fig.width to manually set the figure dimensions, but this does not allow for the auto-sizing as desired.

Hope to get help with this.

Upvotes: 1

Views: 924

Answers (2)

carverd
carverd

Reputation: 420

Check out the answers here. Added the leaflet parameter for this specific use case.

Increase width of entire HTML Rmarkdown output

Basically create a style.css in the same location as your file to define the container width.

div.main-container {
  max-width: 1600px !important;
}

Reference in your yaml

output:
  html_document:
    css: style.css

when you create you leaflet object utilizing the width parameter

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

You could probably do this with inline CSS, but I haven't played around with that.

Visual Examples from an RMD to html. Box plot does not change size.

standard output from rmd. enter image description here

css and leaflet parameter applied enter image description here

Upvotes: 1

Jumble
Jumble

Reputation: 1178

In order to scale the map to the full size of the browser window (as shown in the example you gave here), you need to publish the map directly from the preview window, and not from an Rmarkdown document.

enter image description here

Upvotes: 2

Related Questions