knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(leaflet)
library(leaflet.multiopacity)
library(raster)
# Create raster example
r <- raster(xmn = -2.8, xmx = -2.79,
            ymn = 54.04, ymx = 54.05,
            nrows = 30, ncols = 30)
values(r) <- matrix(1:900, nrow(r), ncol(r), byrow = TRUE)
crs(r) <- CRS("+init=epsg:4326")

This package provides several ways to display opacity controls. The most basic use is calling addOpacityControls() function without any argument.

leaflet() %>%
  addProviderTiles("OpenStreetMap", layerId = "base") %>%
  addRasterImage(r, layerId = "rast") %>%
  addAwesomeMarkers(lng = -2.79545, lat = 54.04321,
                    label = "Hospital", layerId = "hospital") %>%
  addOpacityControls()

Note that each function call has its own layerId. If you don't provide a layerId for your layers, the opacity controls will still work, but will use the internal _leaflet_id as a label for each control.

You can also customize some interface aspects such as box title, position, size and whether opacity controls will remain collapsed.

leaflet() %>%
  addProviderTiles("OpenStreetMap", layerId = "base") %>%
  addRasterImage(r, layerId = "rast") %>%
  addAwesomeMarkers(lng = -2.79545, lat = 54.04321,
                    label = "Hospital", layerId = "hospital") %>%
  addOpacityControls(collapsed = TRUE, position = "topleft", size = "s")

You can also define the subset of layers you want to show opacity controls to. You can provide the layerId of the layers to be shown.

leaflet() %>%
  addProviderTiles("OpenStreetMap", layerId = "osm") %>%
  addRasterImage(r, layerId = "rast") %>%
  addAwesomeMarkers(lng = -2.79545, lat = 54.04321,
                    layerId = "hospital", label = "hospital") %>%
  addOpacityControls(layerId = c("rast", "hospital"))

Or the groups, if you have groups defined.

leaflet() %>%
  addProviderTiles("OpenStreetMap", layerId = "osm", group = "base") %>%
  addRasterImage(r, layerId = "rast", group = "rasters") %>%
  addAwesomeMarkers(lng = -2.79545, lat = 54.04321,
                    layerId = "hospital", label = "hospital",
                    group = "markers") %>%
  addOpacityControls(group = c("base", "rasters", "markers"))

Or even category. For example, if you want to show only rasters (category "image") or markers (category "marker"), you could specify the categories as below.

leaflet() %>%
  addProviderTiles("OpenStreetMap") %>%
  addRasterImage(r, layerId = "rast") %>%
  addAwesomeMarkers(lng = -2.79545, lat = 54.04321,
                    label = "hospital", layerId = "hospital") %>%
  addOpacityControls(category = c("image", "marker"))


meantrix/leaflet.multiopacity documentation built on May 26, 2021, 7:15 p.m.