how to pass a date from server to ui so i can use it in airDatepickerInput

229 Views Asked by At

my shiny server has a dataset with dates on it. I created a variable that gets the max date and i require to pass it to the UI so i can use it as a maximum limit on the calendar. Im having issues with this. df is outside the server part of the server.R file

SERVER

server <- function(input, output, session) {
  
  date_max = max(as.Date(df.direccionamientos$D_FecDireccionamiento))
  output$date_max = reactive ({ format (date_max, "%Y-%m-%d") })
  outputOptions(output, 'date_max', suspendWhenHidden = FALSE)
  }

UI

ui <- fluidPage(
  tags$head(
    
       
    HTML("<title>Mipres</title>"),
     
      HTML(".shiny-notification {
             position:fixed;
             top: calc(0%);
             left: calc(90%);
             }
             "
      )
      ),
    ), 
    img(src = "saludmia-s.png", height = 110, width = 250),
  
  titlePanel(
    h1("Informes MIPRES", align = "center")),            
    hr(style = "border-top: 1px solid #000000;"),

  fluidRow(
    column(2,
           titlePanel(h3("Parametros de busqueda")),
                      
           titlePanel(h4("Fecha")),

           textOutput("date_max"),
                       
           condition = as.Date("output.date_max"),
           
           airDatepickerInput(
             inputId = "date2",
             #label = "Select range of dates:",
             range = F, 
             autoClose = T,
             value = Sys.Date()+1 ,
             #   Sys.Date()),
             todayButton = F,
             clearButton = T,
             maxDate = condition  ,
             addon = c("none")
           ),
   
  ),
  
      #Table Showing Processed Data
     # tableOutput("mytable"),
    
)
1

There are 1 best solutions below

2
On

One way to do it is to use renderUI to display airDatepickerInput() on the server side. Then you can use the date_max there. Try this

ui <- fluidPage(
  tags$head(
    HTML("<title>Mipres</title>"),
    
    HTML(".shiny-notification {
             position:fixed;
             top: calc(0%);
             left: calc(90%);
             }
             "
    )
  ),
  img(src = "saludmia-s.png", height = 110, width = 250),
  
  titlePanel(
    h1("Informes MIPRES", align = "center")),            
  hr(style = "border-top: 1px solid #000000;"),
  
  fluidRow(
    column(2,
           titlePanel(h3("Parametros de busqueda")),
           
           titlePanel(h4("Fecha")),
           
           textOutput("date_max"),
           
           #condition = as.Date("output.date_max"),
           
           uiOutput("airdatepicker")
           
    ))

)

server <- function(input, output, session) {
  
  date_max = max(as.Date(df.direccionamientos$D_FecDireccionamiento))
  
  output$airdatepicker <- renderUI({
    airDatepickerInput(
      inputId = "date2",
      #label = "Select range of dates:",
      range = F, 
      autoClose = T,
      value = Sys.Date()+1 ,
      #   Sys.Date()),
      todayButton = F,
      clearButton = T,
      maxDate = date_max ,
      addon = c("none")
    )
  })
  
  output$date_max = reactive ({ format (date_max, "%Y-%m-%d") })
  outputOptions(output, 'date_max', suspendWhenHidden = FALSE)
  
}

shinyApp(ui, server)