Are-Pro-Gram-In
Are-Pro-Gram-In

Reputation: 55

For Loop for plotting survival curve in Shiny

I am using the diabetic data set from the survival package in R. Within that dataset, is the column age. If I were to plot time-to-event curves comparing the patients' age, I would have way too many survival curves. I want it so that when I select the "age" button for my input, it splits the age column into 4 groups, so that when I plot the survival curve, I only have 4 curves. I keep getting an error though. Am I writing the loop correctly?

ui <- navbarPage(title = "Diabetic Retinopathy",
                 ### Page 1 ###
                 tabPanel(title = "Survival Curves",
                          sidebarLayout(
                            sidebarPanel(
                              radioButtons(inputId = "comparison",
                                           label = "Select variable you wish to compare",
                                           choices = c("laser", "age", "eye", "risk"),
                                           selected = "laser")
                            ),
                            mainPanel(
                              tabsetPanel(
                                tabPanel(title = "Plot", 
                                         plotOutput(outputId = "curve")),
                                tabPanel(title = "Summary", 
                                         verbatimTextOutput(outputId = "details"))
                              )
                            )
                          )
                 )
      )

server <- function(input, output, session) {
  ### Page 1 ###
  fit <- reactive(
    surv_fit(as.formula(paste("Surv(time, status) ~ ", 
                              if(input$comparison == "age") {
                                for(i in diabetic$age) {
                                  if(i < 15) {
                                    diabetic$age[i] <- "lessthan15"
                                  } else if(i >= 15 & i < 30) {
                                    diabetic$age[i]<- "fifteento30"
                                  } else if(i >= 30 & i < 45) {
                                    diabetic$age[i] <- "thirtyto45"
                                  } else {diabetic$age[i] <- "fortyfiveplus"}
                                } 
                              } else {return(input$comparison)})), data = diabetic)
         )
  
  output$curve <- renderPlot(
    ggsurvplot(fit())
  )
}

shinyApp(ui, server)

Error message picture here

Upvotes: 0

Views: 122

Answers (1)

user18309711
user18309711

Reputation:

in surv_fit(as.formula(paste("Surv(time, status) ~ ", etc. two things got mixed up:

  1. pasting the predictor (laser, age, ...) from the value of input$comparison
  2. manipulating the age column of dataframe diabetic - which however does not return any meaningful formula term

You could add a column age_class to your data right from the start and use this as an option for your comparison picker.

PS: to convert numeric (age) to factor (age_class) without if-else-skyscrapers:

df$age_class <- cut(df$age, 
                    breaks=c(-Inf, 15, 30, 45, Inf),
                    labels=c("lessthan15","fifteento30",
                             "thirtyto45","fortyfiveplus")
                    )

Upvotes: 1

Related Questions