|
| 1 | +library(shiny) |
| 2 | +library(plotly) |
| 3 | + |
| 4 | +reusableUI <- function(id = NULL) { |
| 5 | + ns <- NS(id) |
| 6 | + |
| 7 | + fluidRow( |
| 8 | + column(4, plotlyOutput(ns("p1"))), |
| 9 | + column(4, plotlyOutput(ns("p2"))), |
| 10 | + column(4, verbatimTextOutput(ns("ev1"))), |
| 11 | + column(4, verbatimTextOutput(ns("ev2"))) |
| 12 | + ) |
| 13 | +} |
| 14 | + |
| 15 | +viz <- function(input, output, session, src) { |
| 16 | + |
| 17 | + # if you want, you can distinguish between events *within* a module |
| 18 | + src2 <- paste0(src, "2") |
| 19 | + |
| 20 | + output$p1 <- renderPlotly({ |
| 21 | + plot_ly(mtcars, x = ~mpg, y = ~disp, |
| 22 | + key = row.names(mtcars), source = src) |
| 23 | + }) |
| 24 | + output$p2 <- renderPlotly({ |
| 25 | + plot_ly(mtcars, x = ~mpg, y = ~disp, |
| 26 | + key = row.names(mtcars), source = src2) |
| 27 | + }) |
| 28 | + output$ev1 <- renderPrint({ |
| 29 | + event_data("plotly_hover", source = src) |
| 30 | + }) |
| 31 | + output$ev2 <- renderPrint({ |
| 32 | + event_data("plotly_hover", source = src2) |
| 33 | + }) |
| 34 | + |
| 35 | +} |
| 36 | + |
| 37 | +ui <- fluidPage( |
| 38 | + reusableUI("one"), |
| 39 | + reusableUI("two") |
| 40 | +) |
| 41 | + |
| 42 | +server <- function(input, output, session) { |
| 43 | + # use the src argument to namespace plotly events |
| 44 | + callModule(viz, "one", src = "A") |
| 45 | + callModule(viz, "two", src = "B") |
| 46 | +} |
| 47 | + |
| 48 | +shinyApp(ui, server) |
0 commit comments