plotly / plotly.R

An interactive graphing library for R
https://plotly-r.com
Other
2.56k stars 626 forks source link

event_data() triggering unexpected warning about registering #1528

Closed bklingen closed 5 years ago

bklingen commented 5 years ago

Using event_data("plotly_hover", source="mysource") seems to trigger the following warning with the newest plotly version: Warning: The 'plotly_hover' event tied a source ID of 'mysource' is not registered. In order to obtain this event data, please add event_register(p, 'plotly_hover') to the plot (p) that you wish to obtain event data from.

library(shiny)
library(plotly)

ui <- fluidPage(
        plotlyOutput("myplot"),
        verbatimTextOutput("hovered")
      )

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

    output$myplot <- renderPlotly({
        p <- plot_ly(faithful, x=~eruptions, y=~waiting, source="mysource")
        return(p)
    })

    output$hovered <- renderPrint({
      event_data("plotly_hover", source="mysource")
    })

}

shinyApp(ui = ui, server = server)
bklingen commented 5 years ago

The relevant entry from the plotly source code:


# It's possible for event_data() to execute before any 
  # relevant input values have been registered (i.e, before 
  # relevant plotly graphs have been executed). Therefore, 
  # we delay checking that a relevant input value has been 
  # registered until shiny flushes
  session$onFlushed(
    function() {
      eventIDRegistered <- eventID %in% session$userData$plotlyShinyEventIDs
      if (!eventIDRegistered) {
        warning(
          "The '", event, "' event tied a source ID of '", source, "' ",
          "is not registered. In order to obtain this event data, ", 
          "please add `event_register(p, '", event, "')` to the plot (`p`) ",
          "that you wish to obtain event data from.",
          call. = FALSE
        )
      }
    }
  )```
cpsievert commented 5 years ago

Huh, the warning seems to go away if you avoid return()

library(shiny)
library(plotly)

ui <- fluidPage(
  plotlyOutput("myplot"),
  verbatimTextOutput("hovered")
)

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

  output$myplot <- renderPlotly({
    plot_ly(faithful, x=~eruptions, y=~waiting, source="mysource")
  })

  output$hovered <- renderPrint({
    event_data("plotly_hover", source="mysource")
  })

}

shinyApp(ui = ui, server = server)
baj12 commented 5 years ago

How would you handle returning NULL? I need to stop plot when there is no data (or other criteria are not matched). Currently, I return(NULL), but I guess this is not optimal. I would rather see a message saying that data needs be loaded, but I still haven't figured out how to do this... And yes, I have the impression that without the return I see fewer warnings, but I still see some, most probably due to the return(NULL)... Thanks a lot. B

dchen71 commented 5 years ago

Are there any docs on how to use event_register? I recently upgrade to the newest plotly r version and am getting event_register errors on all of my scatter plots. I have two different sources for two different plots.

Edit: I figured it out but it would be helpful if there were additional documentation and examples outside of the webinar for this update.

cpsievert commented 5 years ago

How would you handle returning NULL?

Avoid calling event_data() until the relevant output is non-NULL, like this https://github.com/ropensci/plotly/issues/1538#issuecomment-495312022

If someone can some with an example where this is sufficiently hard, I might be willing to add an official way to suppress the warning in event_data()

Are there any docs on how to use event_register?

Sort of, see ?event_register. It's worth noting though that you shouldn't need to use event_register() unless you need an event that isn't registered by default...here's a hack to get at the default events:

p <- plotly_build(plot_ly())
p$x$shinyEvents
 [1] "plotly_hover"          
 [2] "plotly_click"          
 [3] "plotly_selected"       
 [4] "plotly_relayout"       
 [5] "plotly_brushed"        
 [6] "plotly_brushing"       
 [7] "plotly_clickannotation"
 [8] "plotly_doubleclick"    
 [9] "plotly_deselect"       
[10] "plotly_afterplot" 
CDorich commented 3 years ago

I am still running into this issue of _Warning: The 'plotly_hover' event tied a source ID of 'mysource' is not registered. In order to obtain this event data, please add event_register(p, 'plotlyhover') to the plot (p) that you wish to obtain event data from. I've copied Carson's code from this post, and mixed it with my own editable plotly in a test shiny app and can get it to work. When I run it in a larger app I am getting the same warning but the observe() or event_data("plotly_relayout") does not seem to be working with this app. I can get the shape to move but no underlying data is changing. I am completely stuck about how to get an event_register() to work and why I am not seeing any observe occur with the edited plot.... any guidance on how to code in an event_register() or why I can't see any interaction on this plot?

samssann commented 2 years ago

You could give the warning a class so that these specific warnings could be turned off.

  session$onFlushed(
    function() {
      eventIDRegistered <- eventID %in% session$userData$plotlyShinyEventIDs
      if (!eventIDRegistered) {
        rlang::warn(
          paste0("The '", event, "' event tied a source ID of '", source, "' ",
          "is not registered. In order to obtain this event data, ", 
          "please add `event_register(p, '", event, "')` to the plot (`p`) ",
          "that you wish to obtain event data from."),
          class = "plotly_unregistered_event_warning"
        )
      }
    }
  )

These warnings can be disabled in the shiny server code by wrapping this around the desired code

suppressWarnings(..., classes = "plotly_unregistered_event_warning")
sanjmeh commented 2 years ago

I started getting this warning as soon as I started using source argument in plot_ly(). Seems there is no clear cut solution to this as I see above. Is there any?

davidseres commented 2 years ago

You could give the warning a class so that these specific warnings could be turned off.

  session$onFlushed(
    function() {
      eventIDRegistered <- eventID %in% session$userData$plotlyShinyEventIDs
      if (!eventIDRegistered) {
        rlang::warn(
          paste0("The '", event, "' event tied a source ID of '", source, "' ",
          "is not registered. In order to obtain this event data, ", 
          "please add `event_register(p, '", event, "')` to the plot (`p`) ",
          "that you wish to obtain event data from."),
          class = "plotly_unregistered_event_warning"
        )
      }
    }
  )

These warnings can be disabled in the shiny server code by wrapping this around the desired code

suppressWarnings(..., classes = "plotly_unregistered_event_warning")

Where does eventID come from?

dvg-p4 commented 9 months ago

How would you handle returning NULL?

Avoid calling event_data() until the relevant output is non-NULL, like this #1538 (comment)

If someone can some with an example where this is sufficiently hard, I might be willing to add an official way to suppress the warning in event_data()

Are there any docs on how to use event_register?

Sort of, see ?event_register. It's worth noting though that you shouldn't need to use event_register() unless you need an event that isn't registered by default...here's a hack to get at the default events:

@cpsievert I'm a bit confused, if the recommended way to avoid this warning is a req() above the call, why is the warning message itself talking about event_register()?

dvg-p4 commented 9 months ago

Also, the req() method isn't working consistently for me in a larger app. IIUC when the renderPlotly() and the reactive holding event_data() are both depending on the same reactive, there's no guarantee as to which will be executed first when that reactive becomes available--so if the shiny reactive queue happens to execute the event_data first, we see the warning.

asadow commented 2 weeks ago

@dvg-p4

Hmm, maybe this will help.