【问题标题】:how to pass a date from server to ui so i can use it in airDatepickerInput如何将日期从服务器传递到 ui,以便我可以在 airDatepickerInput 中使用它
【发布时间】:2021-08-10 00:40:24
【问题描述】:

我闪亮的服务器上有一个带有日期的数据集。我创建了一个获取最大日期的变量,我需要将它传递给 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"),
    
)

【问题讨论】:

    标签: r shiny shinywidgets


    【解决方案1】:

    一种方法是使用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)
    

    【讨论】:

    • 不是我期待的方法,但我会尝试一下。谢谢
    • 另一种方法是在ui 端保留airDatepickerInput(),在server 端使用updateAirDateInput
    猜你喜欢
    • 2015-03-11
    • 2019-03-26
    • 2013-02-12
    • 1970-01-01
    • 2015-06-21
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多