I'm using event_data like:
clickData = event_data("plotly_clicked", source = paste0("heatmap_", heatmapName))
Is it possible to find what source
triggered the event? In my case it is not always known. The elements in clickData
are curveNumber
, pointNumber
, x
, and y
.
Edit: It seems what is happening is the last plot created in a loop gets treated as the source for all click events from the other plots. Maybe there is something wrong with the logic in my code. Example below: add plots by adding selections and click plots to see what source triggered the click - always the last plot added.
library(shiny)
library(ggplot2)
library(plotly)
ui = fluidPage(
fluidRow(selectInput("selections", NULL, choices = c("A", "B", "C"), multiple = T)),
fluidRow(uiOutput("plots"))
)
server = function(input, output, session)
{
# Add plot rows for each dataset selected
observe({
output$plots = renderUI({
lapply(input$selections, function(name)
{
fluidRow(plotlyOutput(paste0(name, "_plot")))
})
})
})
# Make plots upon selection
observe({
for(name in input$selections)
{
p = ggplot(mtcars[, c(3,4)]) +
geom_point(aes(x = disp, y = hp))
p_ly = ggplotly(p, source = name)
output[[paste0(name, "_plot")]] = renderPlotly({p_ly})
}
})
# Process clicks
observe({
for(name in input$selections)
{
# Get click data
clickData = event_data("plotly_click", source = name)
if(!is.null(clickData))
{
# Show click source
showNotification(paste0("Clicked ", name))
# Set click to NULL
session$userData$plotlyInputStore[[paste0("plotly_click-", name)]] = NULL
break
}
}
})
}
shinyApp(ui, server)