4

I am trying to get finer control of leaflet popups in R, using the leaflet package. The code for a MWE is below:

library(dplyr)
library(magrittr)
library(leaflet)

download.file(
  url = "http://biogeo.ucdavis.edu/data/gadm2.8/rds/GBR_adm1.rds", 
  destfile = "GBR_adm1.rds", 
  method = "curl"
)

shp_gbr <- readRDS("GBR_adm1.rds")

# get centroids for placing popups in second map
shp_gbr_centers <- 
  rgeos::gCentroid(shp_gbr, byid = TRUE) %>% 
  sp::SpatialPointsDataFrame(shp_gbr@data, match.ID = FALSE)

shp_gbr@data %<>% 
  left_join(shp_gbr_centers[1], by = 'OBJECTID', copy = TRUE) %>% 
  rename(lat = y, lng = x) %>% 
  select(NAME_1, lat, lng) %>% 
  mutate(text = ProgGUIinR::LoremIpsum)

popup <- paste("<b><h3>", shp_gbr$NAME_1, "</h3></b>", shp_gbr$text)

shp_gbr %>% 
  leaflet() %>% 
  addPolygons(popup = ~popup)

This gives a nice map with popups that appear on clicking within the areas of the 4 countries, but in this case, the text is too much for the popup to handle nicely:

enter image description here

What I would like is to access some of the popupOptions available via the addPopups function, in this case to make the popup wider and have a scroll bar. An example of this is below:

shp_gbr %>% 
  leaflet() %>% 
  addPolygons() %>%
  addPopups(
    data = shp_gbr@data,
    popup = ~popup,
    options =
      popupOptions(
        maxWidth = 600,
        maxHeight = 100
      )
  )

However, the popups are now set to be open on launch, rather than appearing on clicking within the boundaries, and do not reopen on click once closed:

enter image description here

My question is how to combine these elements so that you could have, say, a scroll bar for too much text within a map such as the first example, where the popups are closed by default but open on click.

Jonny
  • 2,703
  • 2
  • 27
  • 35
  • Looks like a holdover from how it works in JavaScript. One workaround is to simply get a little more intense with your popup HTML, and add CSS via style attributes to control sizing. – alistaire Feb 18 '17 at 04:37
  • @alistaire Yes, you may be right there... it's just tantalisingly close that it works so well in separate approaches but putting them together doesn't work... – Jonny Feb 18 '17 at 05:03
  • Check the GitHub issues; somebody might have run into this before. If not, you could add one. – alistaire Feb 18 '17 at 05:05
  • 2
    What you are after already works since [this](https://github.com/rstudio/leaflet/pull/261) PR has been merged into the development version. Simply install from github with `devtools` – TimSalabim Feb 18 '17 at 09:47
  • @TimSalabim thanks for pointing this out - that is exactly what I was hoping for, just hadn't installed the latest from Github – Jonny Feb 18 '17 at 11:47

1 Answers1

1

You could use this function to create your popup:

popup <- paste("<div class='leaflet-popup-scrolled' style='max-width:600px;max-height:100px'><b><h3>", shp_gbr$NAME_1, "</h3></b>", shp_gbr$text,"</div>")

It wraps the popup in a div with the leaflet-popup-scrolled class to add the scroll bar and inline CSS to set the max-width and max-height.

NicE
  • 21,165
  • 3
  • 51
  • 68