Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Shiny Object not found in renderPlot

Tags:

r

shiny

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
like image 391
Josh Avatar asked Mar 09 '26 15:03

Josh


1 Answers

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

like image 139
mlegge Avatar answered Mar 11 '26 04:03

mlegge



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!