0

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() 
  })
})    
Daniel D. Sjoberg
  • 8,820
  • 2
  • 12
  • 28
TarJae
  • 72,363
  • 6
  • 19
  • 66

1 Answers1

2

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() 
stefan
  • 90,330
  • 6
  • 25
  • 51