问题描述
目前为止: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()
})
})
解决方法
可以这样实现:
-
您重复了数据集的名称,即
iris2 %>% select(iris2,all_of(varY()))
应该只是iris2 %>% select(all_of(varY()))
-
您还必须选择
by
变量,即select(all_of(c(varY(),varX())))
-
将反应函数直接传递到
by
会出错。因此,我添加了一个辅助变量by
,并将其传递给by
的{{1}}参数。tbl_summary