Skip to contents

This function sets up observers so that:

  1. Clicking a marker on the Leaflet map will select the corresponding row in the DT table.

  2. 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 your leafletOutput. Example: "myMap".

dt_output_id

A character string: the outputId of your DT::DTOutput. Example: "myTable".

shared_id_column

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 by map_lng_col and map_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() (or leaflet::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 their layerId aesthetic mapped to the shared_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 and dt_data_reactive must be reactive expressions (e.g., created with reactive({...})). 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)
} # }