hard to find error in shiny : Warning: Error in FUN: object 'age' not found

It is already hard to find errors in a shiny and the diagnostic messages make it harder. Please see this error:

Warning: Error in FUN: object 'age' not found
  182: FUN
  181: lapply
  180: f
  179: l$compute_aesthetics
  178: f
  177: by_layer
  176: ggplot_build.ggplot
  174: print.ggplot
  166: func
  164: f
  163: Reduce
  154: do
  153: hybrid_chain
  125: drawPlot
  111: <reactive:plotObj>
   95: drawReactive
   82: origRenderFunc
   81: output$totbal
    1: runApp

Looking at above chain I am unable to make out anything as to what could be going wrong.
Since my code is quite long I paste the section of the code that is causing this error.

output$totbal <- isolate(renderPlot({
     surp_end <- data()[[1]]$surplus_bankbal %>% last() # using data() instead of tdata
     if(any(data()[[1]]$surplus_bankbal < 0)) shinyalert("CASH SHORTFALL",text = paste("You will have run out of free cash in year(s):",
                                                                              paste(which(data()[[1]]$surplus_bankbal < 0),collapse = "," )) )
     tdata()[grepl(paste0("total|income|bankbal",input$One_Expense,"|",input$One_Income),variable)] %>% 
       ggplot(aes(age,value)) + 
       geom_line(aes(color=variable),size=1) + 
       geom_point(aes(color = variable),size=3,alpha=0.8) + 
       geom_label(aes(age,value),data = data.frame(x=max(tdata()$age) - 2,y=200), # for single label reduce the data to a single row data.frame
                  label= surp_end %>% round(0) %>% format(big.mark=","),
                  size=12, fill = ifelse(surp_end >0,"green","maroon"), fontface = "bold", color="white",label.size = 1, label.padding = unit(0.7,"lines")) +
       geom_text(aes(x=max(tdata()$age) - 2, y=180), label = "Surplus at death",check_overlap = T) + 
       scale_x_continuous(breaks = 54:95) + 
       scale_y_continuous(limits = c(0,200),breaks = seq(from=0,to = 200,by=10))

Any help (even if educated guesses) would be welcome.


I'm going to guess the error is triggering in your call to geom_label(), where you pass in a new data.frame using the data argument, but the mapped aesthetics (age and value) don't appear in that data (only the variables x and y do). You could try renaming x and y in your data.frame, or change the mapping inside the aes() call for geom_label()


Yes indeed it was the aes() and it was quite a foolish thing on my side that I did not see I had reset the data.
Thanks a lot.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.