Simple question ggsurvplot versus plot Shiny

Dear community,

The code below is working, however I want to make a ggsurvplot instead of a simple plot. However, it returns errors when I try to make the ggsurvplot. I have tried everything I know. Does someone know what the problem is?

I would be very grateful.

Kind regards,
R

# clear all:
rm(list = ls())

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

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
ui <- dashboardPage(
  dashboardHeader(title = "Survival analysis MSI mCRC"),
  dashboardSidebar(),
  dashboardBody(
    box(plotOutput("survival_plot"), width = 8),
    box(selectInput('sur_var', 'Factor of Survival', c("BRAF", "RAS")), width = 4
    )
  )
)


#Server
server <- function(input, output){
  
  # Combine the selected variables into a new data frame
  selectedData <- reactive({
    data[, c(input$sur_var)]
  })
  
  # This is a caption that will show on top of the graph; the name will change based on which variable you choose
  output$caption <- renderText({
    paste("Survival Graph of", input$sur_var, sep="\n")
  })
  
  # Running the survival function
  runSur <- reactive({
    survfit(as.formula(paste("Surv(TI_Met_FUP, Death) ~",paste(input$sur_var))),data=data)
  })
  
  
  output$survival_plot <- renderPlot({
    plot(runSur(),
         col=c("red","sky blue"),
         xlab = "Months", 
         ylab = "Overall survival probability",
         xscale=30.5,
         xlim=c(0,50))
  })
  
}

shinyApp(ui, server)

It looks like your plot isn't working because ggsurvplot can't access the terms of the model that were encoded in the string used to create the model formula. Here's a solution

# clear all:
rm(list = ls())

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

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
ui <- dashboardPage(
  dashboardHeader(title = "Survival analysis MSI mCRC"),
  dashboardSidebar(),
  dashboardBody(
    box(plotOutput("survival_plot"), width = 8),
    box(selectInput('sur_var', 'Factor of Survival', c("BRAF", "RAS")), width = 4
    )
  )
)


#Server
server <- function(input, output){
  
  # Combine the selected variables into a new data frame
  selectedData <- reactive({
    
    data[, c(input$sur_var)]
  })
  
  # This is a caption that will show on top of the graph; the name will change based on which variable you choose
  output$caption <- renderText({
    
    paste("Survival Graph of", input$sur_var, sep="\n")
  })
  
  # Running the survival function
  runSur <- reactive({
    form <- formula(paste("Surv(TI_Met_FUP, Death) ~",input$sur_var))
    eval(bquote(survfit(.(form),data=data)))
})
  
  
  output$survival_plot <- renderPlot({
    survminer::ggsurvplot(fit = runSur(),
                          data = data)
    
    # plot(runSur(),
    #      col=c("red","sky blue"),
    #      xlab = "Months", 
    #      ylab = "Overall survival probability",
    #      xscale=30.5,
    #      xlim=c(0,50))
  })
  
}

shinyApp(ui, server)

You are awesome! Thank you, it works perfectly now!

Kind regards,
R

1 Like

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.