r 闪亮的反应 gtsummary table

r shiny reactive gtsummary table

我在你的帮助下解决了 {gtsummary} 的渲染问题后:!再次感谢 stefan, 我尝试在我的应用程序中构建反应性。 在使用 {gtsummary} 构建摘要 table 之后,我想从 select 输入字段传递 y 变量来更改摘要 table。 我收到此错误:没有适用于 'as_factor' 的方法应用于 class 的对象“c('double', 'numeric')” 那超出了我的极限。有人可以帮忙吗? 我的代码:

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,
             # Select variable for y-axis
             selectInput(inputId = "y", 
                         label = "Y-axis:", 
                         choices = names(iris2),
                         selected = "Sepal.Length"),
             gt_output('table')
      )
    )
  ),
  server = function(input, output) {

    varY <- reactive({input$y})
    
    output$table <- render_gt({
      table1 <- tbl_summary(iris2[, varY()]) %>% as_gt() 
  })
})    

问题是 tbl_summary 需要一个数据帧作为它的第一个参数,而你正在传递一个数字向量 iris2[, varY()]。如果我没看错你想要 select 列 varY() 可以通过以下方式实现:

table1 <- tbl_summary(select(iris2, all_of(varY()))) %>% as_gt()