Created
September 19, 2017 18:25
-
-
Save daattali/4ebd7223b19b6872ce9274ceb62ae669 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
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
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