gt_output: Create a *gt* display table output element for Shiny

View source: R/shiny.R

gt_outputR Documentation

Create a gt display table output element for Shiny


Using gt_output() we can render a reactive gt table, a process initiated by using the render_gt() function in the server component of a Shiny app. The gt_output() call is to be used in the Shiny ui component, the position and context wherein this call is made determines the where the gt table is rendered on the app page. It's important to note that the ID given during the render_gt() call is needed as the outputId in gt_output() (e.g., server: ⁠output$<id> <- render_gt(...)⁠; ui: ⁠gt_output(outputId = "<id>"⁠).

We need to ensure that we have the shiny package installed first. This is easily by using install.packages("shiny"). More information on creating Shiny apps can be found on the Shiny website.





Shiny output ID

⁠scalar<character>⁠ // required

An output variable from which to read the table.


An object of class shiny.tag.


Here is a Shiny app (contained within a single file) that (1) prepares a gt table, (2) sets up the ui with gt_output(), and (3) sets up the server with a render_gt() that uses the gt_tbl object as the input expression.


gt_tbl <-
  gtcars |>
  gt() |>
  fmt_currency(columns = msrp, decimals = 0) |>
  cols_hide(columns = -c(mfr, model, year, mpg_c, msrp)) |>
  cols_label_with(columns = everything(), fn = toupper) |>
  data_color(columns = msrp, method = "numeric", palette = "viridis") |>
  sub_missing() |>
  opt_interactive(use_compact_mode = TRUE)

ui <- fluidPage(
  gt_output(outputId = "table")

server <- function(input, output, session) {
  output$table <- render_gt(expr = gt_tbl)

shinyApp(ui = ui, server = server)
This image of a table was generated from the first code example in the `render_gt()` help file.

Function ID


Function Introduced

v0.2.0.5 (March 31, 2020)

See Also

Other Shiny functions: render_gt()

gt documentation built on June 22, 2024, 11:11 a.m.