Beginner question table1 in Shiny

Dear forum users,

This is the first time I use this forum. As a novice with Shiny I am stuck with creating a Table1 in Shiny. When I execute the code with one of my variables in RenderTable instead of 'input$cat_var' , the code runs fine (showing Table1 in a dashboard). However this is not reactive. Therefore I wanted to use 'input$cat_var'. It is probably a very easy question to answer, but I have been going over it for hours and cannot figure it out.

ui <- dashboardPage(
dashboardHeader(title = "Table1"),
dashboardSidebar(),
dashboardBody(
box(tableOutput("T1")),
box(selectInput('cat_var', 'Variable', c("BRAF", "RAS")), width = 4
)
)
)

#Server
server <- function(input, output){

output$T1 = renderTable({
table1(~ Gender + Age + Sidedness + Res_Prim + Adjuv + diffgr + L1WHO | input$cat_var, data=dataset, render.missing=NULL, render.categorical="FREQ (PCTnoNA%)")
})

}

shinyApp(ui, server)

Hi,

Welcome to the RStudio community!

Since we don't have your dataset, or at least a sample of it, it's hard to recreate the issue.
Here is a working example with one of the default datasets (CO2) from R

library(shiny)
library(shinydashboard)
library(table1)

ui <- dashboardPage(
  dashboardHeader(title = "Table1"),
  dashboardSidebar(),
  dashboardBody(
    box(tableOutput("T1")),
    box(selectInput('cat_var', 'Variable', c("Type", "Treatment")), width = 4
    )
  )
)

#Server
server <- function(input, output){
  
  output$T1 = renderTable({
    table1(~ conc + uptake | input$cat_var, data=CO2)
  })
  
}

shinyApp(ui, server)

If you need help on how to generate a reprex (to share data + code), read this guide. Shiny debugging and reprex guide

PJ

1 Like

Thank you very much for your quick answer PJ! My apologies, in the future I will generate a reprex to make myself more clear. Thank you for helping me :slight_smile:

However, continuing with your example, the problem for me is that table1 doesn't stratify for 'input$cat_var'. In your example I would expect a stratified table1 for Quebec and Mississippi when choosing the variable 'Type' and nonchilled and chilled for using the variable 'Treatment'. I have updated all my packages and restarted R as well. Do you know why I can't see and/or why there is no stratification performed by the table1 function?

Thank you in advance for your time and help

Kind regards,
R

Hi,

I see now what you mean :slight_smile:
The documentation is very confusing and not clear on how to properly do this, but here is a quick fix by making a formula pasting stuff together.

library(shiny)
library(shinydashboard)
library(table1)

ui <- dashboardPage(
  dashboardHeader(title = "Table1"),
  dashboardSidebar(
    selectInput('cat_var', 'Variable', c("Type", "Treatment"))
  ),
  dashboardBody(
    uiOutput("T1")
  )
)

#Server
server <- function(input, output){

  output$T1 = renderUI({
    table1(formula(paste("~ conc + uptake |", input$cat_var)), 
           data=CO2)
  })
  
}

shinyApp(ui, server)

For some reason all the CSS of this table gets lost when seen in the app, resulting in a bad looking layout of the table. I spent a while trying to understand the documentation but it's bad :slight_smile:

PJ

1 Like

Great thank you so much!

Kind regards,
Rudolph

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.