File: app.R

package info (click to toggle)
r-cran-plotly 4.10.4%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 30,636 kB
  • sloc: javascript: 195,272; sh: 24; makefile: 6
file content (39 lines) | stat: -rw-r--r-- 948 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
library(plotly)
library(DT)
library(shiny)
library(crosstalk)

m <- mtcars[, c("mpg", "wt", "disp")] %>% 
  tibble::rownames_to_column()

ui <- fluidPage(
  plotlyOutput("plots"),
  DT::dataTableOutput("table")
)

server <- function(input, output) {
  
  d <- SharedData$new(m, ~rowname)
  
  output$plots <- renderPlotly({
    subplot(
      qplot(data = d, x = mpg, y = wt),
      qplot(data = d, x = mpg, y = disp),
      titleX = T, titleY = T, margin = 0.03
    ) %>% highlight("plotly_selected")
  })
  
  output$table <- DT::renderDataTable({
    m2 <- m[d$selection(),]
    dt <- DT::datatable(m)
    if (NROW(m2) == 0) {
      dt
    } else {
      DT::formatStyle(dt, "rowname", target = "row",
                      color = DT::styleEqual(m2$rowname, rep("white", length(m2$rowname))),
                      backgroundColor = DT::styleEqual(m2$rowname, rep("black", length(m2$rowname))))
    }
  })
}

shinyApp(ui = ui, server = server)