Is it possible to update default/fixed values?


#1

I have an option that enables users to change some default values (a12). If the user chooses to do so and enter new values (a12*), then function (fn1) will be used, if not then the default value of (a12) is used and function (fn2) is used. The problem is in subsequent runs (with only year being changed), fn1 is being used even if the user doesn't change the values a* anymore.
Now I want to be able to update the default values to the new values (a12=a12*), hence any subsequent runs will use fn2. I've tried reactiveValues and so many other things but it doesn't work.
I made a simplified reproducible example below.

        fn1<-function(year1, year2, a){
  table<-data.frame(c(year1,year2,a))
  plotout<-plot(hist(seq(year1:year2)))
  return(list(table,plotout))
}
fn2<-function(year1, year2, a){
  table<-data.frame(c(year1,year2,a^2))
  plotout<-plot(density(seq(year1:year2)))
  return(list(table,plotout))
}
ui <- fluidPage( fluidRow(
                        column(12,sliderInput(inputId="year1",
                                              label="Enter a starting and ending year", value=c(2017,2019),
                                              min=2017, max=2060,sep=""))),

                
                   fluidRow(column(3,numericInput("a12", label="a12 age<65",value=2))),
                 
                 actionButton("update", "Update Table", icon("refresh"),
                              style="color: #fff; background-color: #337ab7"),

                      fluidRow(tableOutput("Table")),
                      fluidRow(plotOutput("plot"))
                      )
                      
server <- function(input, output) {
  p<-eventReactive(input$update,{
    a12<-2
    if(input$a12!=a12){
      tab<-fn1(input$year1[1],input$year1[2],input$a12)
    }else{
      tab<- fn2(input$year1[1],input$year1[2],a12)
    }
    list(tab1=tab[[1]],tab2=tab[[2]])
  }
  )
  output$Table<- renderTable({
    return(p()$tab1)  })
  output$plot<-renderPlot({p()$tab2})
}

shinyApp(ui = ui, server = server)

Thanks