r 闪亮的反应式 gt_summary 表 [英] r shiny reactive gt_summary table

查看:45
本文介绍了r 闪亮的反应式 gt_summary 表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的帮助下解决了 {gtsummary} 的渲染问题后:如何在闪亮的应用程序中使用 {gtsummary} 包 !再次感谢 stefan,我尝试在我的应用程序中构建反应性.使用 {gtsummary} 构建汇总表后,我想从选择输入字段传递 y 变量以更改汇总表.我收到此错误:没有适用于 'as_factor' 的方法应用于类c('double', 'numeric')"这超出了我的极限.有人可以帮忙吗?我的代码:

After I fixed the rendering problem of {gtsummary} with your help: How to use {gtsummary} package in r shiny app !thanks to stefan again, I try to construct reactivity in my app. After construction of the summary table with {gtsummary} I would like to pass the y variable from a select input field to change the summary table. I get this error: no applicable method for 'as_factor' applied to an object of class "c('double', 'numeric')" That exceeds my limits. Can someone please help? My Code:

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()].如果我猜对了,您想选择列 varY() 可以通过以下方式实现:

The issue is that tbl_summary expects a dataframe as its first argument, while your are passing a numeric vector iris2[, varY()]. If I got you right you want to select column varY() which could be achieved by:

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

这篇关于r 闪亮的反应式 gt_summary 表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆