It seems that in the new R plotly 4.9.0 version, there is a change in the event_data code that causes errors if the plot is not yet build.
i.e. a situation where a plot_ly plot is not build because the data it needs is not yet available in the plot by means of a req(data) inside the plot_ly() block. The observeEvent(event_data(“plotly_click”, source = ‘A_plot’), { …}) used to work fine before, but now throws errors, and does so for all forms of event_data.
example of an error message I get now:
Warning: The ‘plotly_click’ event tied a source ID of ‘plotlyplot.testplot’ is not registered. In order to obtain this event data, please add
event_register(p, 'plotly_click')
to the plot (p
) that you wish to obtain event data from.
The app below demonstrates the problem with version 4.9.0:
library(shiny)
library(plotly)
rm(list = ls(), envir = environment()) ## to prevent cross over from old runs
testData = data.frame(day = sample(seq(as.Date('1999/01/01'), as.Date('2000/01/01'), by="day"), 24), frequency = sample(1:5, 24, replace = T ), datecoloring = sample(1:2, 24, replace = T ))
testData$dayPOSIXct <- as.POSIXct(testData$day)
dateRangeMin <- min(testData$day)
dateRangeMax <- max(testData$day)
ui <- fluidPage(
actionButton(inputId = 'Load', label = 'Data'),
plotlyOutput('testplot', width = 700, height = 500)
)
server <- function(input, output,session) {
values <- reactiveValues()
observeEvent(input$Load, {
values$testData <- testData
})
output$testplot <- renderPlotly({
req(values$testData)
p <- plot_ly(data = values$testData, source = 'testplot',
color = as.factor(values$testData$datecoloring), colors = c('#339fff', '#eaf5ff'),
opacity= 0.5, showlegend = T,
marker = list(line = list(width = 2, color = '#0000ff')),
hoverinfo = "text",
text = ~paste('Files:', values$testData$frequency, '<br>Date:', format(values$testData$day, format = '%Y-%m-%d'), sep = ' '))%>%
add_bars( x = ~dayPOSIXct, y = ~frequency, type = "bar", width = 36000000
)
p
})
observeEvent(event_data("plotly_relayout", source = "testplot"),{
#any code here, doesn't matter, bug happens already
})
}
shinyApp(ui, server)