Skip to contents

Replaces the random trace identifiers of a plotly object with simple sequential ones (trace_0, trace_1 and so on).

Usage

simplify_trace_ids(p)

Arguments

p

Plotly object to modify.

Value

A plotly object.

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] "282dd4a667a0b"
names(p2$x$visdat)
#> [1] "282dd3324650"
names(p_deterministic$x$visdat)
#> [1] "trace_0"
names(p2_deterministic$x$visdat)
#> [1] "trace_0"