Two beginner questions Shiny Dashboard

Dear community,

If you could help me with one or both of the following questions, I would be very grateful. I have created a Shiny dashboard for an interactive table1 and survival curve and have two topics I am stuck with for a long time:

  1. Does anyone know how to get the 'print (fit)' into the Shiny Dashboard? Somehow it doesn't work for me.
  2. Does anyone know if the layout/CSS of table1 can be the same in Shiny dashboard as it is outside the Shiny dashboard? The layout changed when it is used in Shiny dashboard. One person of the community has looked into this before, for which I am grateful, but we couldn't figure it out.

If you need more information, please let me know. I would be very happy if I could fix above mentioned problems. Thanks in advance.

R

#Question for the community

library(shiny)
library(shinydashboard)
library(survminer)
library(survival)
library(table1)

Gender <- c("Male", "Male", "Male","Female", "Female", "Female","Female", "Female", "Female","Female", "Female", "Female")
Sidedness <- c("Left","Left","Left","Left","Left","Left","Right","Right","Right","Right","Right","Right")
Nationality <- c("French","French","French","French","French","German","German","German","German","German","German","German")
BRAF <- c("Wildtype","Wildtype","Mutated","Mutated","Mutated","Wildtype","Wildtype","Wildtype","Wildtype","Mutated","Mutated","Mutated")
RAS <- c("Mutated","Mutated","Mutated","Mutated","Mutated","Wildtype","Wildtype","Wildtype","Wildtype","Wildtype","Wildtype","Wildtype")
TI_Met_FUP <- c(5,20,100,60,40,30,30,2,180,270,40,200)
Death <- c(0,1,1,1,1,0,0,0,0,1,1,0)
data <- data.frame(Gender,Sidedness,Nationality,BRAF,RAS,TI_Met_FUP,Death)

ui <- dashboardPage(
  dashboardHeader(title = "Project"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Welcome", tabName = "Welcome", icon = icon("door-open")),
      menuItem("Table1", tabName = "Table1", icon = icon("table")),
      menuItem("OS", tabName = "OS", icon = icon("chart-line"))
    )
  ),
  
  dashboardBody(
    tabItems(
      tabItem("Welcome",
              fluidPage(
                h1("Welcome to the Shiny Dashboard"))
      ),
      tabItem("Table1",
              fluidPage(
                box(uiOutput("T1")),
                box(selectInput('cat_var', 'Variable', c("Gender", "Sidedness", "Nationality")))
              )
      ),
      
      tabItem("OS",
              titlePanel("Survival Data"),
              selectInput(inputId = "BRAF",label="Select BRAF:",c("All", unique(as.character(data$BRAF)))),
              selectInput(inputId = "RAS",label="Select RAS:", c("All", unique(as.character(data$RAS)))),
              plotOutput("p1"),
              box(plotOutput("p2"))
      )
    )))


#Server
server <- function(input, output){
  
  output$T1 = renderUI({
    table1(formula(paste("~ Gender + Sidedness |", input$cat_var)), data=data,render.missing=NULL,
           render.categorical="FREQ (PCTnoNA%)")
  })
  
  filter=reactive({
    filteredData=data
    
    if(input$BRAF != "All") {data <- data[data$BRAF == input$BRAF,]}
    if(input$RAS != "All") {data <- data[data$RAS == input$RAS,]}
    
    filteredData=data
    return(filteredData)
  })
  
  output$p1=renderPlot({
    fit <- survfit(Surv(TI_Met_FUP,Death)~1,data=filter())
    ggsurvplot(fit,data=filter(),xlim=c(0,50), break.time.by =5,
               title=paste(
                 input$BRAF,
                 input$RAS),
               xlab="Time (Months)",
               risk.table=TRUE,
               surv.median.line = "hv",
               ggtheme=theme(plot.title=element_text(hjust=0.5)))
    
  }) 
  output$p2=renderPlot({   
    fit <- survfit(Surv(TI_Met_FUP,Death)~1,data=filter())
    print(fit)
  })
  
}

shinyApp(ui, server)
  1. If you are just wanting the output of print(fit) you need to use renderPrint() in the server and verbatimTextOutput() in your ui, rather than renderPlot() and plotOutput() as you are now.

  2. The CSS that comes included with the table1 package isn't currently being loaded in to your app, so it is defaulting to the bootstrap table class that comes with shiny. You can fix this by including the table1 default css in your app with the following code in your UI:

...,
dashboardBody(
    
    includeCSS(system.file("table1_defaults_1.0", "table1_defaults.css", package = "table1")), # INCLUDE table1 CSS STYLING
    
    tabItems(...

Perfect, it worked!

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.