Skip to content

Instantly share code, notes, and snippets.

@daattali
Created September 19, 2017 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daattali/4ebd7223b19b6872ce9274ceb62ae669 to your computer and use it in GitHub Desktop.
Save daattali/4ebd7223b19b6872ce9274ceb62ae669 to your computer and use it in GitHub Desktop.
# Clicking on a single point seems to clear the lasso selection. But the opposite is not true: doing a lasso selection still keeps the clicked point information.
# 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
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)
@daattali
Copy link
Author

Ideally I'd want to do this directly using plotly, not by having to use reactiveValues to store the previous selections. I want to know if plotly itself has a way to not clear the lasso selection, just like it doesn't clear the clicked point

@cpsievert
Copy link

Whenever I've needed to maintain state like this I use a observeEvent(event_data(...), { modify reactiveValues() here }) pattern https://github.com/ropenscilabs/eechidna/blob/a9213ae14d082c9baa5c64f77c021f37dcbb417d/R/app.R#L213-L253

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment