Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

All event_data() reactives needlessly fire whenever a new one is registered #2337

Closed
dvg-p4 opened this issue Feb 1, 2024 · 1 comment · Fixed by #2339
Closed

All event_data() reactives needlessly fire whenever a new one is registered #2337

dvg-p4 opened this issue Feb 1, 2024 · 1 comment · Fixed by #2339

Comments

@dvg-p4
Copy link
Contributor

dvg-p4 commented Feb 1, 2024

This appears to be due to this line, which (I presume unintentionally) creates a reactive dependency on the names of this reactiveValues list (not just the specific relevant value):

eventHasStorage <- eventID %in% names(session$userData$plotlyInputStore)

I believe this is a mainly an issue if you try to avoid the infamous "event tied a source ID" warning by preventing various event_data() calls from running until after their respective plots have (probably) rendered, as recommended.

Reprex

library(shiny)
library(plotly)
options(shiny.reactlog = TRUE)

ui <- fluidPage(
  fluidRow(
    column(4, checkboxInput("faithful_on", "Render `faithful` plot")),
    column(4, checkboxInput("iris_on", "Render `iris` plot")),
    column(4, checkboxInput("cars_on", "Render `cars` plot")),
  ),
  fluidRow(
    column(4, plotlyOutput("faithful_plot")),
    column(4, plotlyOutput("iris_plot")),
    column(4, plotlyOutput("cars_plot")),
  ),
  fluidRow(
    column(4, verbatimTextOutput("faithful_click")),
    column(4, verbatimTextOutput("iris_click")),
    column(4, verbatimTextOutput("cars_click")),
  )
)

server <- function(input, output, session) {

  output$faithful_plot <- renderPlotly({
    req(input$faithful_on)
    plot_ly(faithful, x=~eruptions, y=~waiting, type = "scatter", mode = "markers", source="faithful")
  })

  output$faithful_click <- renderPrint({
    req(input$faithful_on)
    list(
      updated_at = Sys.time(),
      data = event_data("plotly_click", source="faithful")
    )
  })

  output$iris_plot <- renderPlotly({
    req(input$iris_on)
    plot_ly(iris, x = ~Sepal.Length, y = ~Petal.Length, type = "scatter", mode = "markers", source = "iris")
  })

  output$iris_click <- renderPrint({
    req(input$iris_on)
    list(
      updated_at = Sys.time(),
      data = event_data("plotly_click", source="iris")
    )
  })

  output$cars_plot <- renderPlotly({
    req(input$cars_on)
    plot_ly(cars, x = ~speed, y = ~dist, type = "scatter", mode = "markers", source = "cars")
  })

  output$cars_click <- renderPrint({
    req(input$cars_on)
    list(
      updated_at = Sys.time(),
      data = event_data("plotly_click", source="cars")
    )
  })
}

shinyApp(ui = ui, server = server)

Click on one of the checkboxes to enable a plot and its respective event_data(). Then click on a second. Note the "time updated" on the second plot's output dump as you enable the third plot--it will change, as names(session$userData$plotlyInputStore) triggers a spurious update. (If you refresh and click on a point on the first plot before enabling the second, the first one will spuriously update as well). We can see this dependency in the reactlog:

image image
@dvg-p4 dvg-p4 changed the title This line creates an unnecessary reactive dependency that causes event_data() reactives to fire whenever a new handler is registered event_data() reactives needlessly fire whenever a new handler is registered Feb 1, 2024
@dvg-p4 dvg-p4 changed the title event_data() reactives needlessly fire whenever a new handler is registered All event_data() reactives needlessly fire whenever a new one is registered Feb 1, 2024
dvg-p4 added a commit to dvg-p4/plotly.R that referenced this issue Feb 5, 2024
@dvg-p4
Copy link
Contributor Author

dvg-p4 commented Apr 26, 2024

I've got a very simple PR that will fix this, if someone would be willing to take the time to review it.

cpsievert pushed a commit that referenced this issue Apr 27, 2024
…2339)

* Isolate names(session$userData$plotlyInputStore) to prevent spurious updates (#2337)

* Update NEWS.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant