Utility function to automatically add required parameters to a plotly object for reliable linking, regardless of plot structure (single/multiple traces).
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
# }