Error: object 'data_survival_curve' not found

shiny

#1

Hello friends,

I can not figure out what is wrong with the following code. After running runApp('script.R'), I am getting the following error Error: object 'data_survival_curve' not found. I run debug in RStudio and on line nr 60 this variable is created and it exists till the moment when error comes.

script.R file:

library(shiny)
library(survival)
library(survminer)
library(directlabels)

data <- read.csv('dataset.csv', header = TRUE, sep = ",", fileEncoding="UTF-8")
unique_transplant_years_decreasing <- as.numeric(sort(unique(c(data$transplant_year)), decreasing = TRUE))

krivkaPreziti <- sidebarLayout(
  # all inputs for graph krivka preziti
  sidebarPanel(
    sliderInput("krivka_preziti_input_years", 'Years:', 
                min = unique_transplant_years_decreasing[length(unique_transplant_years_decreasing)],
                max = unique_transplant_years_decreasing[1],
                value = c(unique_transplant_years_decreasing[length(unique_transplant_years_decreasing)],
                          unique_transplant_years_decreasing[1]),
                step = 1),
    numericInput('krivka_preziti_input_seskupit_po', 'Group by (years):',
                 value = 0,
                 min = 0),
    checkboxInput('krivka_preziti_input_facet', 'Facet', value = FALSE),
    width = 3
  ),
  
  # Create a spot for bar plot
  mainPanel(
    h2('Survival curve'),
    br(),
    plotOutput('krivka_preziti', height = "750px"),
    width = 12
  )
)



panelAnalyzaPreziti <- tabPanel(
  'Survival analysis',
  krivkaPreziti 
)


ui <- navbarPage(
  title = "Application",
  panelAnalyzaPreziti
)


server <- shinyServer(
  function(input, output, session)
  {
    ################################# TAB 4 ########################################
    output$krivka_preziti <- renderPlot(
      {
        krivka_year_bottom <- input$krivka_preziti_input_years[1]
        krivka_year_top <- input$krivka_preziti_input_years[2]
        krivka_seskupit_po <- input$krivka_preziti_input_seskupit_po
        
        # data which fit the range of selected years
        # data which meet the condition that survival_time is not NA
        data_survival_curve <- data[data$transplant_year %in% seq(krivka_year_bottom, krivka_year_top) &
                                      !is.na(data$survival_time) &
                                      data$survival_time >= 0,]
        
        
        # if seskupit_po != 0, then cut 
        if(krivka_seskupit_po != 0) {
          data_survival_curve$time_period <- cut(as.numeric(data_survival_curve$transplant_year),
                                                   seq(krivka_year_bottom, krivka_year_top, krivka_seskupit_po),
                                                   include.lowest = T)
          data_survival_curve <- data_survival_curve[!is.na(data_survival_curve$time_period),]
          data_survival_curve$time_period <- as.factor(data_survival_curve$time_period)
        }
        else {
          data_survival_curve$time_period = data_survival_curve$transplant_year
        }
        
        # validate number of rows of data set > 0
        shiny::validate(
          need(nrow(data_survival_curve) > 0, 'Broader your input')
        )
        
        surv_obj <- Surv(data_survival_curve$survival_time/365,data_survival_curve$patient_died)
        fit <- survfit(surv_obj ~ time_period, data = data_survival_curve)
        
        krivka_preziti_plt <- ggsurvplot(fit,
                                         linetype = c('solid'),
                                         ggtheme = theme_bw(),
                                         surv.scale = 'percent',
                                         xlab = 'Roky',
                                         ylab = '%',
                                         censor = FALSE,
                                         break.x.by = 1,
                                         break.y.by = 0.1)
        plot2 <- krivka_preziti_plt + geom_dl(aes(label = time_period), method = list("last.points"), cex = 0.8)
        
        plot2
        
      }
    )
  }
)

shinyApp(
  ui = ui,
  server = server
)

Here is the data set that I am using:


#2

When you get an error one of the things you should do is a search on fragments of the error message you received. For example searching for "Error: object 'data_survival_curve' not found" finds a discussion on Stack Overflow that sounds like it is similar to the problem you are running into:


#3

@danr that's my question on stackoverflow and nobody helped me with that yet.


#4

Sorry, just wanted to make sure all bases were covered...


#5

No problem, thank you. I just hope anyone will know what to do with this..