Shiny App Issue in R “output object not found”

I am facing an issue while creating my Shiny App.

The error message is

Error in output$raw_data <- renderTable({ : object 'output' not found

which occurs for all output codes.

The below codes contain two parts of the shinny app in R which are UI and server. I self-checked the UI part and it seemed fine while running.

I looked up some posts and solutions of them. They suggested I check if all outputs are in the server function, which I did and turned out that all outputs should be inside the server function now. But the issue “output object not found” still recurs.

library(rsconnect)
library(shiny)
library(survival)
library(survminer)
library(readxl)

myData <- structure(list(Years_Diff_Surg_Death = c(36.99383984, 
2.584531143, 36.91991786, 
36.89527721, 36.88158795, 36.82135524), 
Survival = c(1L, 0L, 1L, 1L, 1L, 1L)))

data_2013 <- structure(list(Years_Diff_Surg_Death = c(36.993839835729, 2.5845311430527, 
36.9199178644764, 36.8952772073922, 36.8815879534565, 36.8213552361396), 
Survival = c(1, 0, 1, 1, 1, 0)))

data_2014 <- structure(list(Years_Diff_Surg_Death = c(0.542094455852156, 5.19644079397673, 
35.9561943874059, 
35.917864476386, 35.8658453114305, 
35.8275154004107), 
Survival = c(0, 
0, 1, 1, 1, 1)))

data_2015 <- structure(list(Years_Diff_Surg_Death = c(34.4476386036961, 34.2559890485969, 
0.621492128678987, 34.3874058863792, 
34.3326488706365, 1.08145106091718), Survival = c(0, 
1, 1, 0, 1, 0)))

data_2016 <- structure(list(Years_Diff_Surg_Death = c(2.90212183436003, 
0.950034223134839, 
33.9301848049281, 33.9110198494182, 33.8726899383984, 
33.8535249828884), 
Survival = c(1, 1, 0, 0, 0, 0)))

data_2017 <- structure(list(Years_Diff_Surg_Death = c(32.9911019849418, 3.34839151266256, 
32.952772073922, 32.911704312115, 32.8761122518823, 0.791238877481177), 
Survival = c(1, 0, 1, 1, 1, 1)))

set.seed(121)
mydate = seq(Sys.Date(), by = "day", length.out = 1800)
mystatus = sample(c("DATE_OF_SURGERY", "DATE_OF_DEATH"), 30, replace = TRUE)
mydf = data.frame(Date = mydate, Status = mystatus)



# Define UI for dataset viewer application
ui <- shinyUI(
  
  pageWithSidebar(
  # Application title.
  headerPanel(""),
  
  sidebarPanel(
    sliderInput(inputId = "surv",
                label = "Number of Years",
                min = 0,
                max = 5,
                value = 1,
                animate = animationOptions(interval=800, loop=TRUE)
    )
  ),
  
  mainPanel(
    h1("Survival Plot"),
    img(src = "logo-no-tag.png", height = 120, weight = 200),
    fluidPage(
      
      # Copy the line below to make a date selector 
      dateRangeInput(
        inputId = "daterange",
        label = "select the date range",
        start = min(mydf$Date),
        end = max(mydf$Date),
        min = min(mydf$Date),
        max = max(mydf$Date),
        format = "yyyy/mm/dd",
        separator = "to"
      ),
      textOutput("startdate"),
      textOutput("enddate"),
      textOutput("range"),
      tableOutput("subdata"),

      hr(),
      fluidRow(column(3, verbatimTextOutput("value"))),
      fluidRow(column(3, verbatimTextOutput("value"))),
    ),
    
    tabsetPanel(
      tabPanel("Plot", plotOutput("plot_predicted")),
      tabPanel("Model Summary", verbatimTextOutput("summary")),
      tabPanel("Data", tableOutput("raw_data")),
      tabPanel("Survival Curves for Each Year", plotOutput("plot_predicted_2")),
      id = "tabs"
    ) 
  )
))

survfitdata4 <- survfit(Surv(Years_Diff_Surg_Death, Survival) ~ 1, data = myData)

    server <- function(input, output, session) { 
  output$value <- renderPrint({ input$date })

  output$plot_predicted <- renderPlot({
    survobj1 <- with(myData, Surv(Years_Diff_Surg_Death, Survival))
    fit1 <- survfit(survobj1~1, data = myData)
    ggsurvplot(fit1, conf.int = TRUE,
               ylim = c(0,1),
               xlim = c(0,5),
               break.x.by = 1,
               title = ("Years of Death After Surgery via Survival"),
               xlab = ("Years"),
               legend = "none")
  })
  
  output$plot_predicted_2 <- renderPlot({
    survobj_2013 <- with(data_2013, Surv(Years_Diff_Surg_Death, Survival))
    survobj_2014 <- with(data_2014, Surv(Years_Diff_Surg_Death, Survival))
    survobj_2015 <- with(data_2015, Surv(Years_Diff_Surg_Death, Survival))
    survobj_2016 <- with(data_2016, Surv(Years_Diff_Surg_Death, Survival))
    survobj_2017 <- with(data_2017, Surv(Years_Diff_Surg_Death, Survival))
    surv_2013 <- survfit(Surv(Years_Diff_Surg_Death, Survival) ~ 1, data = data_2013)
    surv_2014 <- survfit(Surv(Years_Diff_Surg_Death, Survival) ~ 1, data = data_2014)
    surv_2015 <- survfit(Surv(Years_Diff_Surg_Death, Survival) ~ 1, data = data_2015)
    surv_2016 <- survfit(Surv(Years_Diff_Surg_Death, Survival) ~ 1, data = data_2016)
    surv_2017 <- survfit(Surv(Years_Diff_Surg_Death, Survival) ~ 1, data = data_2017)
    fit_comb <- list(s_2013 = surv_2013,
                     s_2014 = surv_2014,
                     s_2015 = surv_2015,
                     s_2016 = surv_2016,
                     s_2017 = surv_2017)
    ggsurvplot_combine(fit_comb,
                       data_ECV,
                       xlab = ("Years"),
                       xlim = c(0,5),
                       break.x.by = 1)
  })
  
  output$summary <- renderPrint({
    summary(survfitdata4)
  })
  
  output$raw_data <- renderTable({
    myData
  }, include.rownames = FALSE)
  
  # returns the start date selected by the user
  output$startdate <- renderText({
    as.character(input$daterange[1]) # start date selected by user
  })
  
  # returns the end date selected by the user
  output$enddate <- renderText({
    as.character(input$daterange[2]) # End date selected by the user
  })
  
  # returns the text showing the date range selected by the user
  output$range <- renderText({
    paste("Selected date range is ", input$daterange[1], "to", input$daterange[2]) 
  })
  
  # returns the status count information based on selected date
  output$subdata <- renderTable({
    # subsetting using the dates selected by the user
    s = subset(mydf, mydf$Date>= input$daterange[1] & mydf$Date<= input$daterange[2])
    table(s$Status) # to get the counts on status for selected date range
    
  })
}

Leave a Comment