How to use {gtsummary} package in r shiny app

1.1k views Asked by At

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.

1

There are 1 answers

1
stefan On BEST ANSWER

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)
  })