【问题标题】:r shiny {gtsummary} by= argument reactive from a second SelectInputr shiny {gtsummary} by= 来自第二个 SelectInput 的响应参数
【发布时间】:2021-02-08 19:52:41
【问题描述】:

到目前为止:r shiny reactive gt_summary table

我想要一个 gtsummary 表,其中包含以反应方式 (Input$y) 从 SelectInput 字段中选择的变量。这已经实现了。 现在我想从第二个反应式 SelectInput 字段 (Input$x) 中为 gtsummary 选择 by= 参数。尝试了很多但没有成功。感谢您的帮助。

我的代码:

library(shiny)
library(gtsummary)
library(gt)
# make dataset with a few variables to summarize
iris2 <- iris %>% select(Sepal.Length,  Sepal.Width, Species)
# add fake factor column 
iris2 <- iris2 %>% 
  mutate(Species_1 = Species)


shinyApp(
  ui = fluidPage(
    fluidRow(
      column(12,
             # Select variable to analyze
             selectInput(inputId = "y", 
                         label = "Y-Variable:", 
                         choices = c("Sepal Length" ="Sepal.Length",
                                    "Sepal Width" = "Sepal.Width"),
                         selected = "Sepal.Length"),
             
             # select factor variable
             selectInput(inputId = "x",
                         label = "Factor:",
                         choices = c("Species" = "Species", 
                                   "Other Species" = "Species_1"),
                         selected = "Species"),
             
             gt_output('table')
      )
    )
  ),
  server = function(input, output) {

    varY <- reactive({
      input$y
      })
    varX <- reactive({
      input$x
      })
    
    output$table <- render_gt({
      
      table1 <- iris2 %>% select(iris2, all_of(varY())) %>%
        tbl_summary(by = varX()) %>%
        add_p(pvalue_fun = ~style_pvalue(.x, digits = 2)) %>% as_gt()
  })
      
}) 

【问题讨论】:

    标签: r shiny reactive gtsummary gt


    【解决方案1】:

    这可以这样实现:

    1. 您重复了数据集的名称,即 iris2 %&gt;% select(iris2, all_of(varY())) 应该只是 iris2 %&gt;% select(all_of(varY()))

    2. 您还必须选择by 变量,即select(all_of(c(varY(), varX())))

    3. 将反应函数直接传递给by 会出错。因此我添加了一个辅助变量by,我将它传递给tbl_summaryby 参数。

      output$table <- render_gt({
        by <- varX()
        table1 <- iris2 %>% 
          select(all_of(c(varY(), by))) %>%
          tbl_summary(by = by) %>%
          add_p(pvalue_fun = ~style_pvalue(.x, digits = 2)) %>% as_gt()
      })
      

    【讨论】:

    • 亲爱的 stefan,非常感谢。我无语了。我该如何表达我的感激之情?
    • 嗨塔杰。不客气。总是很高兴能提供帮助,对我来说,这是一个了解 gtsummary 和闪亮的更多信息的好机会。最佳 S.
    猜你喜欢
    • 2021-06-13
    • 1970-01-01
    • 1970-01-01
    • 2017-01-17
    • 2011-05-27
    • 2020-04-02
    • 2020-09-14
    • 2021-05-17
    • 2021-08-04
    相关资源
    最近更新 更多