Scott Bradshaw
Scott Bradshaw

Reputation: 80

Use of popupOptions() in leaflet package

I am following the leaflet tutorial on Robin Lovelace's blog (http://robinlovelace.net/r/2015/02/01/leaflet-r-package.html). My code below works fine:

# Install leaflet package
if(!require(leaflet)) install_github("rstudio/leaflet")

# Plot a default web map (brackets display the result)
m <- leaflet() %>% addTiles(m,urlTemplate = "http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png")

m %>% setView(lng = -1.5, lat = 53.4, zoom = 10) # set centre and extent of map

m2 <- m %>%
   setView(-1.5, 53.4, 10) %>% # map location
   addMarkers(-1.4, 53.5) %>% # add a marker
   addPopups(-1.6, 53.3, popup = "Hello Sheffield!",options=list(closeButton=FALSE))
m2

But the options for the addPopups is enclosed in the addPopup bit. How do I use the popupOptions() command?

I thought it would be this:

# Plot a default web map (brackets display the result)
m <- leaflet() %>% addTiles(m,urlTemplate = "http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png")

m %>% setView(lng = -1.5, lat = 53.4, zoom = 10) # set centre and extent of map

m2 <- m %>%
   setView(-1.5, 53.4, 10) %>% # map location
   addMarkers(-1.4, 53.5) %>% # add a marker
   addPopups(-1.6, 53.3, popup = "Hello Sheffield!") %>%
  popupOptions(closeButton=FALSE) ##NEW BIT
m2

But popupOptions isn't used in the map (the x box is there).

What am I doing wrong?

Upvotes: 3

Views: 6830

Answers (1)

Spacedman
Spacedman

Reputation: 94237

Just use popupOptions to create a list of options that then goes into the options arg. It fills in all the other ones with defaults:

leaflet() %>% addTiles() %>%
  addPopups(-1.6, 53.5,popup="Hello Sheffield",options=popupOptions(closeButton=FALSE))

If you just type popupOptions(closeButton=FALSE) you'll see the big list that is getting passed to options= in the example.

Upvotes: 2

Related Questions