addReactiveFeatures | R Documentation |
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
.
addReactiveFeatures(
map,
x,
srcLayer,
by,
on,
group,
layerId = NULL,
options = NULL,
style = NULL,
updateStyle = NULL,
popup = NULL,
...
)
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 |
by |
shared attribute between |
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 |
group |
the group name for the object to be added to |
layerId |
the layerId. |
options |
options to be passed to the layer.
See e.g. |
style |
named list of styling instructions for the geometries in |
updateStyle |
named list of how to update the styling of the |
popup |
a character vector of the HTML content for the popups of layer |
... |
currently not used. |
library(leaflet)
library(leafem)
library(sf)
library(geojsonsf)
# create some random data
che = st_as_sf(gadmCHE)
if (require(lwgeom)) {
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)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.