首页 > 解决方案 > 如何将日期从服务器传递到 ui,以便我可以在 airDatepickerInput 中使用它

问题描述

我闪亮的服务器上有一个带有日期的数据集。我创建了一个获取最大日期的变量,我需要将它传递给 UI,以便我可以将其用作日历的最大限制。我对此有疑问。df 在 server.R 文件的服务器部分之外

服务器

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 <- 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"),
    
)

标签: rshinyshinywidgets

解决方案


一种方法是使用在服务器端renderUI显示airDatepickerInput()。然后你可以在date_max那里使用。尝试这个

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)

推荐阅读