How to use {gtsummary} package in r shiny app

纵然是瞬间 提交于 2021-02-10 02:29:51

问题


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:


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


来源:https://stackoverflow.com/questions/64197029/how-to-use-gtsummary-package-in-r-shiny-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!