# Usage with leafletProxy

library(leaflet)
library(leaflet.multiopacity)
library(raster)

If you intend to use leaflet.multiopacity with Shiny, it is possible that you use the leafletProxy() function instead of only one call to leaflet() function. If this is your case, and you want to use the opacity controls of this package, you need to set the renderOnLayerAdd argument to TRUE.

ui <- shiny::fluidPage(
leaflet::leafletOutput("map"),
)

server <- function(input, output, session) {

# Create a map inside of your server function
output$map <- leaflet::renderLeaflet({ # Create raster example r <- raster::raster(xmn = -2.8, xmx = -2.79, ymn = 54.04, ymx = 54.05, nrows = 30, ncols = 30) raster::values(r) <- matrix(1:900, nrow(r), ncol(r), byrow = TRUE) raster::crs(r) <- raster::crs("+init=epsg:4326") leaflet::leaflet() %>% leaflet::setView(lng = -2.79545, lat = 54.04321, zoom = 14) %>% leaflet::addProviderTiles("OpenStreetMap", layerId = "osm") %>% leaflet.multiopacity::addOpacityControls(group = "layersToAdd", renderOnLayerAdd = TRUE) }) # Observer that trigger a map update shiny::observeEvent(input$addLayers, {
leaflet::leafletProxy("map", session) %>%
layerId = "raster1",
layerId = "raster2",