4
votes

I'm trying to make a simple shiny ap for creating kaplan-meier survival curves that are stratified by selection the user makes. When I code the KM calculation statically (with the column name thorTr) it works but the calculation and plot is static. When I replace with input$s I get ERROR:variable lengths differ (found for 'input$s')

I've tried looking at other code which use as.formula and paste, but I don't understand and couldn't get to work. But I am a new R and Shiny user so maybe I didn't get it right. Here is a similar shiny ap but I want to use survminer and the ggsurvplot for plotting

library(shiny)
library(ggplot2)
library(survival) 
library(survminer)

#load data
data(GBSG2, package = "TH.data")


#Define UI for application that plots stratified km curves
ui <- fluidPage(

  # Sidebar layout with a input and output definitions
  sidebarLayout(

    # Inputs
    sidebarPanel(

      # Select variable strat
      selectInput(inputId = "s", 
                  label = "Select Stratification Variable:",
                  choices = c("horTh","menostat","tgrade"), 
                  selected = "horTh")

    ),

    # Outputs
    mainPanel(
      plotOutput(outputId = "km")
    )
  )
)

# Define server function required to create the km plot
server <- function(input, output) {

  # Create the km plot object the plotOutput function is expecting
  output$km <- renderPlot({

    #calc KM estimate with a hard coded variables - the following line works but obviously is not reactive
    #km <- survfit(Surv(time,cens) ~ horTh,data=GBSG2)

    #replaced hard coded horTh selection with the respnse from the selection and I get an error
    km <- survfit(Surv(time,cens) ~ input$s ,data=GBSG2)

    #plot km
    ggsurvplot(km)

  })

}

# Create a Shiny app object
shinyApp(ui = ui, server = server)

I expect to have a plot that updates the stratification variable with the users selection

3

3 Answers

2
votes

Two things:

  1. The formula in the call to survfit() needs to be defined explicitly. The object being passed to survfit() in the original code uses a character value on the right hand side of the function. This throws an error, which we can address by translating the entire pasted value into a formula, i.e., as.formula(paste('Surv(time,cens) ~',input$s))
  2. The formula needs to be defined in the call to ggsurvplot() to avoid scoping issues. This is a little more technical and has to do with the way that ggsurvplot() is programmed. Basically, ggsurvplot() can't access a formula that is defined outside of its own call.

Try replacing

km <- survfit(Surv(time,cens) ~ input$s ,data=GBSG2)
ggsurvplot(km)

with

ggsurvplot(survfit(as.formula(paste('Surv(time,cens) ~',input$s)),data=GBSG2))
2
votes

Try using surv_fit() instead of survfit().

surv_fit() is a helper from survminer which does different scoping compared to survival:survit(), which is what you seem to need, as Byron suggests.

My snippet looks like:

output$plot <- renderPlot({

    formula_text <- paste0("Surv(OS, OS_CENSOR) ~ ", input$covariate)

    ## for ggsurvplot, use survminer::surv_fit instead of survival:survfit
    fit <- surv_fit(as.formula(formula_text), data=os_df)
    ggsurvplot(fit = fit, data=os_df)
})
1
votes

Hi finally got this to work combinigng both solutions. I don't understand the fix but at least it now works the way I wanted it to :)

library(shiny)
library(ggplot2)
library(survival) 
library(survminer)

data(GBSG2, package = "TH.data")

# Define UI for application that plots features of movies
ui <- fluidPage(

  # Sidebar layout with a input and output definitions
  sidebarLayout(

    # Inputs
    sidebarPanel(

      # Select variable strat
      selectInput(inputId = "s", 
                  label = "Select Stratification Variable:",
                  choices = c("Hormone Therapy" = "horTh",
                              "Menopausal Status" = "menostat",
                              "Tumor Grade" = "tgrade"), 
                  selected = "horTh")

    ),

    # Outputs
    mainPanel(
      plotOutput(outputId = "km")
    )
  )
)

# Define server function required to create the scatterplot
server <- function(input, output) {

  # Create the km plot object the plotOutput function is expecting
  output$km <- renderPlot({

    ## calc survival curve and plot
    kmdata <- surv_fit(as.formula(paste('Surv(time,cens) ~',input$s)),data=GBSG2)
    ggsurvplot(kmdata)

  })

}

# Create a Shiny app object
shinyApp(ui = ui, server = server)