Skip to content

Commit 0beb525

Browse files
committed
add persistent selection in shiny example for #1187
1 parent ba53a46 commit 0beb525

File tree

1 file changed

+36
-0
lines changed
  • inst/examples/shiny/event_data_persist

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
library(shiny)
2+
library(plotly)
3+
4+
ui <- fluidPage(
5+
plotlyOutput("plot"),
6+
verbatimTextOutput("data")
7+
)
8+
9+
mtcars$id <- row.names(mtcars)
10+
11+
server <- function(input, output, session) {
12+
13+
output$plot <- renderPlotly({
14+
plot_ly(mtcars, x = ~disp, y = ~mpg) %>%
15+
add_markers(key = ~id) %>%
16+
layout(dragmode = "select") %>%
17+
highlight("plotly_selected")
18+
})
19+
20+
selected <- reactiveVal(rep(FALSE, nrow(mtcars)))
21+
22+
selected_data <- reactive({
23+
ed <- event_data("plotly_selected")
24+
if (is.null(ed)) return(NULL)
25+
new <- mtcars[["id"]] %in% ed[["key"]]
26+
selected(selected() | new)
27+
mtcars[selected(), ]
28+
})
29+
30+
output$data <- renderPrint({
31+
selected_data()
32+
})
33+
34+
}
35+
36+
shinyApp(ui, server)

0 commit comments

Comments
 (0)