简体   繁体   中英

Error in event_data(“plotly_hover”)? ( r plotly, shiny)

Within a Shiny app, I'm trying to link multiple plots. To do this, I need to be able to retrieve hover data with something like event_data("plotly_hover") . Although this has worked for me before, today for some reason I've been running into a problem I haven't been able to troubleshoot. When I hover over any plotly object and display the hover event data, this error is returned within the Shiny app:

Warning: Error in cat: argument 1 (type 'list') cannot be handled by 'cat'

In the past, using event_data(...) on a plotly object has worked well for me so I'm left scratching my head about what may be going on. Here is some self-contained sample code:

ui <- fluidPage(
  plotlyOutput("singlePlot"),
  verbatimTextOutput("hoverData")
)

server <- function(input, output, session) {
  output$singlePlot <- renderPlotly({
    p <- plot_ly(x = 1:10, y = 1:10, color = I("red"), marker = list(color = "blue"))
    p
  })

  output$hoverData <- renderText(event_data("plotly_hover"))
}


shinyApp(ui = ui, server = server) 

In theory I should see something like this:

      curveNumber  pointNumber      x      y 
1               0            1      1      4

But I'm left with the error above. Any ideas on what might be going on?

Okay-- I found the solution... kind of silly, but using renderPrint() instead of renderText() works seamlessly. Whoops! Thanks.

I think it is because renderText doesn't know how to treat a dataframe , as the name suggests it renders text, nothing else that does not meet the "string" qualification will work probably. Wrapping it in as.character solves your issue too. renderText( as.character(event_data("plotly_hover")))

ui <- fluidPage(
  plotlyOutput("singlePlot"),
  verbatimTextOutput("hoverData")
)

server <- function(input, output, session) {
  output$singlePlot <- renderPlotly({
    p <- plot_ly(x = 1:10, y = 1:10, color = I("red"), marker = list(color = "blue"))
    p
  })

  output$hoverData <- renderText( as.character(event_data("plotly_hover")))
}


shinyApp(ui = ui, server = server) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM