Skip to contents

A simple interface to link interactive plots and tables in Shiny. This function automatically detects component types and sets up bidirectional linking.

Usage

link_plots(
  session,
  ...,
  shared_id_column,
  leaflet_lng_col = "longitude",
  leaflet_lat_col = "latitude",
  leaflet_click_handler = NULL,
  dt_click_handler = NULL,
  on_selection_change = NULL
)

Arguments

session

The Shiny session object

...

Named arguments where names are component output IDs and values are reactive data frames. Each data frame must contain the shared_id_column.

shared_id_column

Character string naming the column that contains unique identifiers present in all linked components.

leaflet_lng_col

Character string naming the longitude column for leaflet maps. Defaults to "longitude".

leaflet_lat_col

Character string naming the latitude column for leaflet maps. Defaults to "latitude".

leaflet_click_handler

Optional function that handles leaflet marker clicks. This will be used for both direct clicks and when other components select this marker. Function should accept (map_proxy, selected_data, session).

dt_click_handler

Optional function that handles DT row selections. This will be used for both direct clicks and when other components select this row. Function should accept (dt_proxy, selected_data, session).

on_selection_change

Optional callback function that gets called when selection changes. Function should accept parameters: (selected_id, selected_data, source_component_id, session)

Value

Invisibly returns the created registry object

Examples

if (FALSE) { # \dontrun{
# Basic usage with default behaviors
link_plots(
  session,
  myMap = reactive({
    map_data
  }),
  myTable = reactive({
    table_data
  }),
  shared_id_column = "location_id"
)

# With custom leaflet click behavior
link_plots(
  session,
  myMap = reactive({
    map_data
  }),
  myTable = reactive({
    table_data
  }),
  shared_id_column = "location_id",
  leaflet_click_handler = function(map_proxy, selected_data, session) {
    # Custom popup and zoom behavior
    map_proxy %>%
      leaflet::setView(lng = selected_data$longitude, lat = selected_data$latitude, zoom = 15) %>%
      leaflet::addPopups(
        lng = selected_data$longitude,
        lat = selected_data$latitude,
        popup = paste0("<b>", selected_data$name, "</b><br>Custom info here")
      )
  }
)
} # }