I'm using plotly::ggplotly()
and I need the user to be able to both select a single point and to select multiple points with brushing. I want both selection options to exist in parallel. The user should be able to click on a point and to lasso select several points, and both of those pieces of information should be recorded.
The problem I'm having is that if I click on a point, then the lasso selection gets reset. But the opposite is not true: if I lasso select and then click on a point, then both are retained.
Here's a GIF of this issue
Here's my code:
library(shiny)
library(plotly)
ui <- fluidPage(
plotlyOutput("plot"),
verbatimTextOutput("click"),
verbatimTextOutput("brush")
)
server <- function(input, output, session) {
nms <- row.names(mtcars)
output$plot <- renderPlotly({
p <- ggplot(mtcars, aes(x = mpg, y = wt, key = nms)) + geom_point()
ggplotly(p) %>% layout(dragmode = "lasso")
})
output$click <- renderPrint({
d <- event_data("plotly_click")
if (!is.null(d)) d
})
output$brush <- renderPrint({
d <- event_data("plotly_selected")
if (!is.null(d)) d
})
}
shinyApp(ui, server)
To reproduce:
- Click a single point
- Do a lasso selection
- Both are currently visible
- Click a different point
- Now the lasso selection information is gone
- Do a lasso selection again, both are visible again