File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed
inst/examples/shiny/drag_lines Expand file tree Collapse file tree 1 file changed +46
-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(" p" ),
6+ verbatimTextOutput(" event" )
7+ )
8+
9+ x <- seq(- 10 , 10 )
10+ y <- rnorm(length(x ))
11+
12+
13+ server <- function (input , output , session ) {
14+
15+ output $ p <- renderPlotly({
16+
17+ d <- event_data(" plotly_relayout" , source = " trajectory" )
18+
19+ selected_point <- if (! is.null(d [[" shapes[0].x0" ]])) {
20+ xint <- d [[" shapes[0].x0" ]]
21+ xpt <- x [which.min(abs(x - xint ))]
22+ list (x = xpt , y = y [which(x == xpt )])
23+ } else {
24+ list (x = 1 , y = y [which(x == 1 )])
25+ }
26+
27+ plot_ly(color = I(" red" ), source = " trajectory" ) %> %
28+ add_lines(x = x , y = y ) %> %
29+ add_markers(x = selected_point $ x , y = selected_point $ y ) %> %
30+ layout(
31+ shapes = list (
32+ type = " line" ,
33+ line = list (color = " gray" , dash = " dot" ),
34+ x0 = selected_point $ x ,
35+ x1 = selected_point $ x ,
36+ y0 = 0 ,
37+ y1 = 1 ,
38+ yref = " paper"
39+ )
40+ ) %> %
41+ config(editable = TRUE )
42+ })
43+
44+ }
45+
46+ shinyApp(ui , server )
You can’t perform that action at this time.
0 commit comments