File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed
inst/examples/shiny/event_data_persist Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change
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 )
You can’t perform that action at this time.
0 commit comments