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)
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(), ...)
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:
Install (
install.packages("DT")
) and load (library(DT)
) the DT package.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 theoutputId
argument ofdataTableOutput()
to a unique value.Within the server function, call
renderDataTable()
, also from the DT package, and save its output as an element of theoutput
list. Name the element after theoutputId
used above. For example,output$table <- renderDataTable()
.dataTableOutput()
will display the value of theoutput
element whose name matches itsoutputId
.Pass
renderDataTable()
a block of code, surrounded with{}
, that returns either a DataTable widget (usually created withdatatable()
) or data frame that will be automatically passed todatatable()
.Optionally, use
...
to pass additional arguments todatatable()
if you suppliedrenderDataTable()
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)
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)
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)