Skip to content

Key variable returned as length 1 character from event_data('plotly_click') #1708

Open
@tcash21

Description

@tcash21

Not sure if this is a bug or not, but I'm using plotly within a Shiny app. I'm using a geom_tile() heatmap type of graph and the user should be able to click a box which then triggers a follow-up database query. I followed the solution here: https://community.plot.ly/t/how-to-return-the-same-event-data-information-for-selected-points-even-after-modifying-the-data/5847/2

However, this doesn't seem to work for a geom_tile() click. I create a key variable by pasting the label and month of the clicked box together. The key is always returned as a length 1 character instead of returning the full string. Full reprex below:

library(plotly)
library(shiny)

mtcars$key <- row.names(mtcars)
mtcars$col <- "black"
mtcars$name <- row.names(mtcars)

ui <- fluidPage(
  plotlyOutput("plot")
)

server <- function(input, output, session) {
  output$plot <- renderPlotly({
    p <- ggplot(mtcars, aes(x = gear, y = name, key = key)) + 
      geom_tile(aes(fill = mpg, key = key))
    ggplotly(p) 
  })
  
  observeEvent(event_data("plotly_click"), {
    click_data <- event_data("plotly_click")
    print(click_data)
   
  })
}
shinyApp(ui, server)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions