DataTable

#| standalone: true
#| components: [viewer]
#| viewerHeight: 300

library(shiny)
library(bslib)
library(DT)
library(palmerpenguins)
library(dplyr)

ui <- page_fluid(
  dataTableOutput("table") 
)

server <- function(input, output) {
  output$table <- 
    renderDataTable({datatable(penguins)}) 
}

shinyApp(ui = ui, server = server)
library(shiny)
library(bslib)
library(DT)
library(palmerpenguins)
library(dplyr)

ui <- page_fluid(
  dataTableOutput("table") 
)

server <- function(input, output) {
  output$table <- 
    renderDataTable({datatable(penguins)}) 
}

shinyApp(ui = ui, server = server)
No matching items

Relevant Functions

  • dataTableOutput
    dataTableOutput(outputId, width = "100%", height = "auto", fill = TRUE)

  • renderDataTable
    renderDataTable( expr, server = TRUE, env = parent.frame(), quoted = FALSE, funcFilter = dataTablesFilter, future = FALSE, outputArgs = list(), ...)

No matching items

Details

A DataTable presents tabular data in a figure-like view with a minimum of grid lines.

To make a reactive DataTable, follow these steps:

  1. Install (install.packages("DT")) and load (library(DT)) the DT package.

  2. Call dataTableOutput(), from the DT package, in the UI of your app to create a div in which to display the table. Where you call this function within the UI functions will determine where the DataTable will appear within the layout of the app. Set the outputId argument of dataTableOutput() to a unique value.

  3. Within the server function, call renderDataTable(), also from the DT package, and save its output as an element of the output list. Name the element after the outputId used above. For example, output$table <- renderDataTable(). dataTableOutput() will display the value of the output element whose name matches its outputId.

  4. Pass renderDataTable() a block of code, surrounded with {}, that returns either a DataTable widget (usually created with datatable()) or data frame that will be automatically passed to datatable().

  5. Optionally, use ... to pass additional arguments to datatable() if you supplied renderDataTable() with a normal data frame object.

Variations

Select Rows

Set datatable()’s selection argument to "single" to allow the user to only select one row at a time. Set it to “none" to prevent the user from selecting rows. By default, selection is "multiple", allowing the user to select multiple rows at a time.

#| standalone: true
#| components: [viewer]
#| viewerHeight: 350

library(shiny)
library(bslib)
library(DT)
library(palmerpenguins)
library(dplyr)

ui <- page_fluid(
  dataTableOutput("table") 
)

server <- function(input, output) {
  output$table <- 
    renderDataTable({datatable(penguins, selection = "single")}) 
}

shinyApp(ui = ui, server = server)
library(shiny)
library(bslib)
library(DT)
library(palmerpenguins)
library(dplyr)

ui <- page_fluid(
  dataTableOutput("table") 
)

server <- function(input, output) {
  output$table <- 
    renderDataTable({datatable(penguins, selection = "single")}) 
}

shinyApp(ui = ui, server = server)

Filterable Table

Set datatable()’s filter argument to "top" or "bottom" to add a row of filter options. Users can interact with these options to filter the table.

#| standalone: true
#| components: [viewer]
#| viewerHeight: 350

library(shiny)
library(bslib)
library(DT)
library(palmerpenguins)
library(dplyr)

ui <- page_fluid(
  dataTableOutput("table") 
)

server <- function(input, output) {
  output$table <- 
    renderDataTable({datatable(penguins, filter = "top")}) 
}

shinyApp(ui = ui, server = server)
library(shiny)
library(bslib)
library(DT)
library(palmerpenguins)
library(dplyr)

ui <- page_fluid(
  dataTableOutput("table") 
)

server <- function(input, output) {
  output$table <- 
    renderDataTable({datatable(penguins, filter = "top")}) 
}

shinyApp(ui = ui, server = server)

Set Table Size

Set the height and width arguments of dataTableOutput() to constrain the output size of the table.

#| standalone: true
#| components: [viewer]
#| viewerHeight: 350

library(shiny)
library(bslib)
library(DT)
library(palmerpenguins)
library(dplyr)

ui <- page_fluid(
  dataTableOutput("table", height = "250px", width = "300px") 
)

server <- function(input, output) {
  output$table <- 
    renderDataTable({datatable(penguins)}) 
}

shinyApp(ui = ui, server = server)
library(shiny)
library(bslib)
library(DT)
library(palmerpenguins)
library(dplyr)

ui <- page_fluid(
  dataTableOutput("table", height = "250px", width = "300px") 
)

server <- function(input, output) {
  output$table <- 
    renderDataTable({datatable(penguins)}) 
}

shinyApp(ui = ui, server = server)
No matching items