plotly / plotly/plotly.R

HTML documents including plotly plots are not reproducible

Open
#2,074 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
2.7k
Forks
641
PR merge metrics
No merged PRs in 30d

Description

I believe it is known that the value of plotly::plot_ly(x = 1, y = 1)$x$cur_data is hard to reproduce, compare https://github.com/plotly/plotly.R/issues/1048. Some work has been done in #1379 towards use in shinytest at least.

I am also using shinytest, but in my app, users are downloading HTML files with plotly plots generated using rmarkdown::render(). So these files are still non-reproducible because I cannot seem to use renderPlotly() inside rmarkdown::render(). To see the problem:

bug.Rmd:

---
output: html_document
---

```{r}
plotly::plot_ly(x = 1, y = 1)
```

Then run R -q -s -e 'rmarkdown::render("bug.Rmd")' and see the value of cur_data in bug.html change every time you regenerate it.

I see two options:

  • I could replicate the logic of
    https://github.com/plotly/plotly.R/blob/bab01af316673e35d8314129178bfb27eed27fe7/R/shiny.R#L46-L53
    inside bug.Rmd, like this:
    p <- plotly::plot_ly(x = 1, y = 1)
    p$x$attrs <- NULL # and a few more
    p
    
    However, this breaks the plot. Any way I can make this work?
  • I have also tried putting this inside bug.Rmd:
    shiny::snapshotPreprocessOutput(
      plotly::plot_ly(x = 1, y = 1),
      function(value) {
        # https://github.com/plotly/plotly.R/blob/3d49fbc5897703d58b/R/shiny.R#L68
        json <- jsonlite::from_JSON(value)
        json$x <- json$x[setdiff(names(json$x), c("visdat", "cur_data", "attrs"))]
        # https://github.com/plotly/plotly.R/blob/3d49fbc589/R/utils.R#L1020-L1025
        jsonlite::toJSON(json,
          digits = 50, auto_unbox = TRUE, force = TRUE,
          null = "null", na = "null", time_format = "%Y-%m-%d %H:%M:%OS6"
        )
      }
    )
    
    but rmarkdown::render() seemed to ignore it, even inside shinytest (or did I do anything wrong there)?
  • One could amend #1365 to take into account a hash of the current plot serialization to avoid generating identical IDs even if the external seed is reset to the same values externally. This would avoid the need for tempfile().

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with bug.Rmd and R -q -s -e 'rmarkdown::render("bug.Rmd")', then compare the cur_data value in bug.html across renders. Read the referenced logic in R/shiny.R and R/utils.R, along with the related issues #1048, #1379, and #1365. Done means HTML documents rendered with rmarkdown::render() produce reproducible plot data without breaking the plot.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, r
Domain
data-visualization, web-dev
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.