Skip to contents

This function adds a layer to a map that is dependent on another layer. The reactive layer will be shown/hidden when holding the Ctrl-button on your keyboard and performing the action defined by on. on can be either "click" (default) or "mouseover".

Note: srcLayer needs to be added to the map using addGeoJSON because we need to be able to link the two layers by a common attribute defined by argument by. Linking will be done via group name of srcLayer.

Usage

addReactiveFeatures(
  map,
  x,
  srcLayer,
  by,
  on,
  group,
  layerId = NULL,
  options = NULL,
  style = NULL,
  updateStyle = NULL,
  popup = NULL,
  ...
)

Arguments

map

a mapview or leaflet object.

x

the (sf) features to be added to the map.

srcLayer

the group name of the source layer that x should be bound to.

by

shared attribute between x and srcLayer by which the two layers should be bound together.

on

the action to invoke the action. Can be one of "click" (default) and "mouseover". The action will be triggered by holding Ctrl-key and performing on.

group

the group name for the object to be added to map.

layerId

the layerId.

options

options to be passed to the layer. See e.g. pathOptions for details.

style

named list of styling instructions for the geometries in x.

updateStyle

named list of how to update the styling of the srcLayer.

popup

a character vector of the HTML content for the popups of layer x. See addControl for details.

...

currently not used.

Examples

library(leaflet)
library(leafem)
library(sf)
library(geojsonsf)

# create some random data
che = st_as_sf(gadmCHE)
pts = st_as_sf(st_sample(che, 200))
pts = st_join(pts, che[, "ID_1"])

che = sf_geojson(che)

leaflet() %>%
  addTiles() %>%
  addGeoJSON(che, group = "che") %>%
  addReactiveFeatures(
    pts
    , srcLayer = "che"
    , by = "ID_1"
    , on = "click"
    , group = "pts"
    , style = list(color = "black", fillOpacity = 0.3)
    , updateStyle = list(
      opacity = 0.3
      , fillOpacity = 0.3
      , color = "forestgreen"
      , fillColor = "forestgreen"
    )
  ) %>%
  addMouseCoordinates() %>%
  setView(lng = 8.31, lat = 46.75, zoom = 8)