Skip to contents

Utility function to automatically add required parameters to a plotly object for reliable linking, regardless of plot structure (single/multiple traces).

Usage

prepare_plotly_linking(plotly_obj, id_column, source)

Arguments

plotly_obj

A plotly object created with plot_ly()

id_column

Character string: name of the ID column in the data

source

Character string: plotly source identifier

Value

Modified plotly object with linking parameters added

Examples

# \donttest{
   library(plotly)
#> Loading required package: ggplot2
#> 
#> Attaching package: ‘plotly’
#> The following object is masked from ‘package:ggplot2’:
#> 
#>     last_plot
#> The following object is masked from ‘package:stats’:
#> 
#>     filter
#> The following object is masked from ‘package:graphics’:
#> 
#>     layout

  # Sample data
  df <- data.frame(
    id = 1:5,
    value = c(10, 20, 15, 25, 30),
    group = c("A", "A", "B", "B", "C")
  )

  # Create a plotly scatter plot
  p <- plot_ly(
    data = df,
    x = ~value,
    y = ~id,
    color = ~group
  )

  # Prepare for linking (adds customdata and source)
  p <- prepare_plotly_linking(p, "id", "my_plot")
#> Added customdata = ~id to plotly object

  # Print the plot object (for demonstration)
  print(p)
#> No trace type specified:
#>   Based on info supplied, a 'scatter' trace seems appropriate.
#>   Read more about this trace type -> https://plotly.com/r/reference/#scatter
#> No scatter mode specifed:
#>   Setting the mode to markers
#>   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
# }