Create a two-way link between a Leaflet map and a DT datatable.
link_leaflet_dt.Rd
This function sets up observers so that:
Clicking a marker on the Leaflet map will select the corresponding row in the DT table.
Selecting a row in the DT table will highlight the corresponding marker on the Leaflet map and fly the map to that marker.
Usage
link_leaflet_dt(
input,
session,
leaflet_output_id,
dt_output_id,
shared_id_column,
leaflet_data_reactive,
dt_data_reactive,
map_lng_col = "longitude",
map_lat_col = "latitude",
highlight_zoom = 12,
highlight_icon = leaflet::awesomeIcons(icon = "star", library = "glyphicon",
markerColor = "red", iconColor = "#FFFFFF")
)
Arguments
- input
The Shiny
input
object from your server function.- session
The Shiny
session
object from your server function.- leaflet_output_id
A character string: the
outputId
of yourleafletOutput
. Example: "myMap".- dt_output_id
A character string: the
outputId
of yourDT::DTOutput
. Example: "myTable".A character string: the name of the column that is present in both the Leaflet data and the DT data. This column must contain unique identifiers for each item. When creating your Leaflet map, ensure you set the
layerId
for your markers (or other shapes) to the values from this column. Example:addMarkers(..., layerId = ~my_unique_id_column)
.- leaflet_data_reactive
A reactive expression that returns the data frame used to generate the Leaflet map. This data frame MUST contain the
shared_id_column
and the columns specified bymap_lng_col
andmap_lat_col
. Example:reactive({ my_spatial_data_frame })
.- dt_data_reactive
A reactive expression that returns the data frame displayed in the DT table. This data frame MUST contain the
shared_id_column
. Example:reactive({ my_table_data_frame })
.- map_lng_col
(Optional) A character string: the name of the column in
leaflet_data_reactive
that contains the longitude values for the map markers. Defaults to "longitude".- map_lat_col
(Optional) A character string: the name of the column in
leaflet_data_reactive
that contains the latitude values for the map markers. Defaults to "latitude".- highlight_zoom
(Optional) An integer: the zoom level the map will fly to when a marker is highlighted. Defaults to 12.
- highlight_icon
(Optional) An icon object created by
leaflet::awesomeIcons()
(orleaflet::makeAwesomeIcon()
,leaflet::icons()
) to use for the highlighted marker on the map. Defaults to a red star icon.
Value
This function does not return a value. It is called for its side effects, which are to set up the reactive observers that link the map and table.
Important Notes for Users
layerId
is Crucial: For the map-to-table link to work, your Leaflet markers (or circles, polygons, etc.) must have theirlayerId
aesthetic mapped to theshared_id_column
. For example:leaflet() %>% addMarkers(data = my_data, layerId = ~my_id_column, ...)
Unique IDs: The values in your
shared_id_column
should be unique for each location/item to ensure correct linking.Data Reactivity: Both
leaflet_data_reactive
anddt_data_reactive
must be reactive expressions (e.g., created withreactive({...})
). This allows the linking to work even if your underlying data changes.DT Selection Mode: For best results, set your DT table to single row selection:
DT::datatable(..., selection = 'single')
. The function currently focuses on the first selected row if multiple are somehow selected.
Examples
if (FALSE) { # \dontrun{
# Running an app we have included in the package
fn_app <- system.file(
"examples", "link_plots.R",
package = "rbranding"
)
shiny::runApp(fn_app)
} # }