Replaces the random trace identifiers of a plotly object with simple sequential ones (trace_0
, trace_1
and so on).
Arguments
- p
Plotly object to modify.
Details
This function is especially useful to apply before a plotly object or its JSON representation (as generated for rendered R Markdown / Quarto documents) is version controlled (e.g. via Git).
To also make the htmlwidget identifier reproducible when rendering a Plotly chart, use htmlwidgets::setWidgetIdSeed()
.
Examples
p <- plotly::plot_ly(data = mtcars,
x = ~mpg,
type = "histogram")
p2 <- plotly::plot_ly(data = mtcars,
x = ~mpg,
type = "histogram")
p_deterministic <- plotlee::simplify_trace_ids(p)
p2_deterministic <- plotlee::simplify_trace_ids(p2)
names(p$x$visdat)
#> [1] "35b5a57667b6e"
names(p2$x$visdat)
#> [1] "35b5a11ecb026"
names(p_deterministic$x$visdat)
#> [1] "trace_0"
names(p2_deterministic$x$visdat)
#> [1] "trace_0"