|
| 1 | +library(plotly) |
| 2 | +library(shiny) |
| 3 | + |
| 4 | +# user interface |
| 5 | +ui <- fluidPage( |
| 6 | + titlePanel("Linked highlighting with plotly and shiny"), |
| 7 | + mainPanel( |
| 8 | + htmltools::div(style = "display:inline-block", plotlyOutput("x", width = 400, height = 250)), |
| 9 | + wellPanel( |
| 10 | + style = "display:inline-block; vertical-align:top;", |
| 11 | + sliderInput("xbins", "Number of x bins", |
| 12 | + min = 1, max = 50, value = 20, width = 250), |
| 13 | + sliderInput("ybins", "Number of y bins", |
| 14 | + min = 1, max = 50, value = 20, width = 250) |
| 15 | + ), |
| 16 | + br(), |
| 17 | + htmltools::div(style = "display:inline-block", plotlyOutput("xy", width = 400, height = 400)), |
| 18 | + htmltools::div(style = "display:inline-block", plotlyOutput("y", width = 250, height = 400)) |
| 19 | + ) |
| 20 | +) |
| 21 | + |
| 22 | +# marker objects |
| 23 | +m <- list(color = toRGB("black")) |
| 24 | +m2 <- list(color = toRGB("black", 0.2)) |
| 25 | + |
| 26 | +server <- function(input, output, session) { |
| 27 | + |
| 28 | + # convenience function for computing xbin/ybin object given a number of bins |
| 29 | + compute_bins <- function(x, n) { |
| 30 | + list( |
| 31 | + start = min(x), |
| 32 | + end = max(x), |
| 33 | + size = (max(x) - min(x)) / n |
| 34 | + ) |
| 35 | + } |
| 36 | + |
| 37 | + # the 'x' histogram |
| 38 | + output$x <- renderPlotly({ |
| 39 | + x <- cars$speed |
| 40 | + xbins <- compute_bins(x, input$xbins) |
| 41 | + p <- plot_ly(x = x, type = "histogram", autobinx = F, |
| 42 | + xbins = xbins, marker = m2) |
| 43 | + # obtain plotlyjs selection |
| 44 | + s <- event_data("plotly_selected") |
| 45 | + # if points are selected, subset the data, and highlight |
| 46 | + if (length(s$x) > 0) { |
| 47 | + p <- add_trace(p, x = s$x, type = "histogram", autobinx = F, |
| 48 | + xbins = xbins, marker = m) |
| 49 | + } |
| 50 | + p %>% |
| 51 | + config(displayModeBar = F, showLink = F) %>% |
| 52 | + layout(showlegend = F, barmode = "overlay", yaxis = list(title = "count"), |
| 53 | + xaxis = list(title = "", showticklabels = F)) |
| 54 | + }) |
| 55 | + |
| 56 | + # basically the same as 'x' histogram |
| 57 | + output$y <- renderPlotly({ |
| 58 | + y <- cars$dist |
| 59 | + ybins <- compute_bins(y, input$ybins) |
| 60 | + p <- plot_ly(y = y, type = "histogram", autobiny = F, |
| 61 | + ybins = ybins, marker = m2) |
| 62 | + s <- event_data("plotly_selected") |
| 63 | + if (length(s$y) > 0) { |
| 64 | + p <- add_trace(p, y = s$y, type = "histogram", autobiny = F, |
| 65 | + ybins = ybins, marker = m) |
| 66 | + } |
| 67 | + p %>% |
| 68 | + config(displayModeBar = F, showLink = F) %>% |
| 69 | + layout(showlegend = F, barmode = "overlay", xaxis = list(title = "count"), |
| 70 | + yaxis = list(title = "", showticklabels = F)) |
| 71 | + }) |
| 72 | + |
| 73 | + output$xy <- renderPlotly({ |
| 74 | + cars %>% |
| 75 | + plot_ly(x = speed, y = dist, |
| 76 | + mode = "markers", marker = m) %>% |
| 77 | + layout(dragmode = "select") |
| 78 | + }) |
| 79 | + |
| 80 | +} |
| 81 | + |
| 82 | +shinyApp(ui, server) |
0 commit comments