Skip to contents

link_plots is a simple interface to link interactive plots and tables in Shiny. This function automatically detects component types and sets up bidirectional linking. For more robust applications, especially with complex naming schemes, consider using register_leaflet and register_dt directly.

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. For leaflet maps: can be sf objects (coordinates auto-extracted) or regular data frames with longitude/latitude columns.

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". For sf objects, this will be the name of the created column.

leaflet_lat_col

Character string naming the latitude column for leaflet maps. Defaults to "latitude". For sf objects, this will be the name of the created column.

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 (interactive()) {
  library(shiny)
  library(leaflet)
  library(DT)

  # Sample data
  sample_data <- data.frame(
    id = 1:10,
    name = paste("Location", 1:10),
    latitude = runif(10, 40.7, 40.8),
    longitude = runif(10, -111.95, -111.85),
    value = round(runif(10, 100, 1000))
  )

  ui <- fluidPage(
    titlePanel("linkeR Example"),
    fluidRow(
      column(6, leafletOutput("my_map")),
      column(6, DTOutput("my_table"))
    )
  )

  server <- function(input, output, session) {
    my_data <- reactive({
      sample_data
    })

    output$my_map <- renderLeaflet({
      leaflet(my_data()) %>%
        addTiles() %>%
        addMarkers(
          lng = ~longitude,
          lat = ~latitude,
          layerId = ~id,
          popup = ~name
        )
    })

    output$my_table <- renderDT({
      datatable(my_data()[, c("name", "value")], selection = "single")
    })

    link_plots(
      session,
      my_map = my_data,
      my_table = my_data,
      shared_id_column = "id"
    )
  }

  shinyApp(ui, server)
}