Show Sidebar Hide Sidebar

Datatable and Plotly in R

Getting Started with Datatable and Plotly

New to Plotly?

Plotly's R library is free and open source!
Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode.
We also have a quick-reference cheatsheet (new!) to help you get started!

Version Check

Version 4 of Plotly's R package is now available!
Check out this post for more information on breaking changes and new features available in this version.

library(plotly)
packageVersion('plotly')

Datatable and Plotly App

Code

library(shiny)
library(DT)
library(plotly)
library(crosstalk)

m <- mtcars %>% 
  tibble::rownames_to_column()

ui <- fluidPage(
  h1("Plotly & DT", align = "center"),
  plotlyOutput("x2"),
  DT::dataTableOutput("x1"),
  fluidRow(
    p(class = 'text-center', downloadButton('x3', 'Download Filtered Data'))
  )
)

server <- function(input, output) {

  d <- SharedData$new(m, ~rowname)

  # highlight selected rows in the scatterplot
  output$x2 <- renderPlotly({

    s <- input$x1_rows_selected

    if (!length(s)) {
      p <- d %>%
        plot_ly(x = ~mpg, y = ~disp, mode = "markers", color = I('black'), name = 'Unfiltered') %>%
        layout(showlegend = T) %>% 
        highlight("plotly_selected", color = I('red'), selected = attrs_selected(name = 'Filtered'))
    } else if (length(s)) {
      pp <- m %>%
        plot_ly() %>% 
        add_trace(x = ~mpg, y = ~disp, mode = "markers", color = I('black'), name = 'Unfiltered') %>%
        layout(showlegend = T)

      # selected data
      pp <- add_trace(pp, data = m[s, , drop = F], x = ~mpg, y = ~disp, mode = "markers",
                      color = I('red'), name = 'Filtered')
    }

  })

  # highlight selected rows in the table
  output$x1 <- 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))))
    }
  })

  # download the filtered data
  output$x3 = downloadHandler('mtcars-filtered.csv', content = function(file) {
    s <- input$x1_rows_selected
    if (length(s)) {
      write.csv(m[s, , drop = FALSE], file)
    } else if (!length(s)) {
      write.csv(m[d$selection(),], file)
    }
  })

}

shinyApp(ui, server)
Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.