Skip to contents

register_plotly registers a Plotly component for linking with other components. The default behavior uses plotly's built-in point selection highlighting, which is simple and works reliably across all plot types.

Usage

register_plotly(
  session,
  registry,
  plotly_output_id,
  data_reactive,
  shared_id_column,
  event_types = c("plotly_click"),
  source = NULL,
  click_handler = NULL
)

Arguments

session

Shiny session object

registry

A link registry created by create_link_registry()

plotly_output_id

Character string: the outputId of your plotlyOutput

data_reactive

Reactive expression returning the data frame for the plot

shared_id_column

Character string: name of the ID column

event_types

Character vector: plotly event types to listen for

source

Character string: plotly source identifier for event tracking

click_handler

Optional function: custom selection update handler. Function signature: function(plot_proxy, selected_data, session) where selected_data is the row from data_reactive() or NULL to clear selection.

Value

NULL (invisible). This function is called for its side effects.

Examples

# \donttest{
  # Create a mock session for the example
  session <- shiny::MockShinySession$new()

  # Create a registry
  registry <- create_link_registry(session)

  # Sample reactive data
  my_data <- shiny::reactive({
    data.frame(
      id = 1:5,
      name = c("A", "B", "C", "D", "E"),
      value = 11:15
    )
  })

  # Register a plotly component
  register_plotly(
    session,
    registry,
    plotly_output_id = "my_plot",
    data_reactive = my_data,
    shared_id_column = "id"
  )
#> * plotly component 'my_plot' registered for linking
#> * linkeR will automatically handle linking for most plot configurations
#> * If clicking doesn't work, add: customdata = ~id to your plot_ly() call

  # Verify registration
  print(registry$get_components())
#> $`mock-session-my_plot`
#> $`mock-session-my_plot`$type
#> [1] "plotly"
#> 
#> $`mock-session-my_plot`$shared_id_column
#> [1] "id"
#> 
#> $`mock-session-my_plot`$config
#> $`mock-session-my_plot`$config$event_types
#> [1] "plotly_click"
#> 
#> $`mock-session-my_plot`$config$source
#> [1] "my_plot"
#> 
#> $`mock-session-my_plot`$config$click_handler
#> NULL
#> 
#> 
#> 
# }