1
votes

I defined 'Time' in renderPlot(shiny) function. However, I get an

Error: object 'Time' not found.

I wonder that why I get this error as I already defined for the 'Time' variable

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

ui <- fluidPage(
   sidebarLayout(
      sidebarPanel(
         fileInput("file","File input"),

         selectInput("Survival",
                     label = "Survival",
                     choices =  c('time.day',
                                  'time.year'),
                     selected = 'time.day'),
         selectInput("Treatment",
                     "Treatment",
                     choices = c("trt"),
                     selected = "trt")
      ),
      mainPanel(
        plotOutput("KM")
       )
   )
)

server <- function(input, output) {
    output$KM <- renderPlot({  
      req(input$file)
      df <- read.csv(input$file$datapath)
      df<- data.frame(df)
    Time <- switch(input$Survival, 
                  "time.day" = df$time.day,
                  "time.year" = df$time.yr)
    x <- switch(input$Treatment,
                    "trt" = df$trt)

    fit <- survfit(Surv(Time , cen) ~ x , data = df)
    ggsurvplot(fit,risk.table = TRUE)
      }
   )  
}

shinyApp(ui = ui, server = server)

The input data usually a csv file like:

 time.yr       time.day     cen  trt
1.181940686 431.7038355 0   1
3.174982816 1159.662473 1   1
1
It is completely irreproducible. In which part of your code did you define Time? Also, R is case-sensitive to if you define time (which is most likely the case as per "common" naming conventions in R programming) then time != Time. If that doesn't solve your issue, please provide a meaningful reproducible example - should be a relatively easy fix based on your question. - onlyphantom
Sorry, it is because of the automatic format transformation when I copy paste my code, and I already corrected my post. - Josh
Can you give me a simple reproducible example? I have a hunch but need to make sure my solution correctly solves your issue before I paste it as an answer. - onlyphantom
Sorry for the trouble, I believe it can be reproed now. - Josh

1 Answers

0
votes

This turned out to be a tricky problem. Usually we would suggest some non-standard evaluation to create the fit but it seems that ggsurvplot is finicky and its much easier to just create the exact data frame you need:

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

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      fileInput("file","File input"),

      selectInput("Survival",
                  label = "Survival",
                  choices =  c('time.day',
                               'time.year'),
                  selected = 'time.day'),

      selectInput("Treatment",
                  "Treatment",
                  choices = c("trt"),
                  selected = "trt")
    ),
    mainPanel(
      plotOutput("KM")
    )
  )
)

server <- function(input, output) {

  raw_surv_data <- reactive({
    req(input$file)
    read.csv(input$file$datapath)
  })

  surv_data <- reactive({
    raw_surv <- raw_surv_data()
    data.frame(
      Time = raw_surv[[input$Survival]],
      x    = raw_surv[[input$Treatment]],
      cen  = raw_surv[['cen']]
    )
  })

  surv_fit <- reactive({
    survfit(Surv(Time , cen) ~ x , data = surv_data())
  })

  output$KM <- renderPlot({  
    ggsurvplot(surv_fit(), risk.table = TRUE, data = surv_data())
  })  
}

shinyApp(ui = ui, server = server)

enter image description here