Search code examples
rshinygtsummary

How to use {gtsummary} package in r shiny app


Is it possible to render a table with {gtsummary} in a shiny app?

library(gtsummary)
# make dataset with a few variables to summarize
iris2 <- iris %>% select(Sepal.Length,  Sepal.Width, Species)

# summarize the data with our package
table1 <- tbl_summary(iris2)
table1

in a Shiny app: ->

shinyApp(
ui = fluidPage(
  fluidRow(
    column(12,
      tableOutput('table')
    )
  )
),
server = function(input, output) {
  output$table <- renderTable(table1)
})  

Thank you.


Solution

  • Maybe this what you are looking for. To render a gt table in a shiny app you have to make use of gt::gt_output and gt::render_gt. To make this work for your gtsummary table you have to convert it to gt table via as_gt():

    library(shiny)
    library(gtsummary)
    library(gt)
    # make dataset with a few variables to summarize
    iris2 <- iris %>% select(Sepal.Length,  Sepal.Width, Species)
    
    # summarize the data with our package
    table1 <- tbl_summary(iris2) %>% as_gt()
    table1
    
    shinyApp(
      ui = fluidPage(
        fluidRow(
          column(12,
                 gt_output('table')
          )
        )
      ),
      server = function(input, output) {
        output$table <- render_gt(table1)
      })