【问题标题】:Implementing checkbox to select all date columns in Shiny实现复选框以选择 Shiny 中的所有日期列
【发布时间】:2020-10-02 04:26:31
【问题描述】:

我有一个如下所示的数据集:

 lat      lon        2020-01-22 2020-01-23 2020-01-24 2020-01-25 2020-01-26 2020-01-27
 32.53953 -86.64408           0          0          0          0          0          0
 30.72775 -87.72207           0          0          0          0          0          0
 31.86826 -85.38713           0          0          0          0          0          0
 32.99642 -87.12511           0          0          0          0          0          0
 33.98211 -86.56791           0          0          0          0          0          0
 32.10031 -85.71266           0          0          0          0          0          0

我正在开发一个闪亮的应用程序,它允许用户使用 dateRangeInput 指定日期范围。但是,我还想为用户提供使用复选框输入选择所有可用日期的选项,但我不确定如何实现这一点。这是我目前所拥有的:

ui <- fluidPage(    

  sidebarLayout(
    sidebarPanel(
      dateRangeInput("date", "Date Range:",
                     start = as.character(Sys.Date() - 30),
                     end = as.character(Sys.Date())),
      checkboxInput("checkBox", "Select all dates", FALSE),
      textOutput("warning")
    ),

    mainPanel(
         ## Plot & Table will go here
    )
  )
)

server <- function(input, output) {
  
  output$warning <- renderText({
    validate(
      need(input$date[2] > input$date[1], "Error in date specification")
    )
  })

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    要为所有可能的日期选择一个日期范围,您需要指定minmax。然后,使用updateDateRangeInput()https://shiny.rstudio.com/reference/shiny/latest/updateDateInput.html

    library(shiny)
    
    ui <- fluidPage(    
      
      sidebarLayout(
        sidebarPanel(
          dateRangeInput("date", "Date Range:",
                         start = as.character(Sys.Date() - 30),
                         end = as.character(Sys.Date()),
                         min = "2020-01-01", 
                         max =Sys.Date()),
          checkboxInput("checkBox", "Select all dates", FALSE),
          textOutput("warning")
        ),
        
        mainPanel(
          ## Plot & Table will go here
        )
      )
    )
    
    server <- function(input, output, session) {
      
      observe({
        if (input$checkBox==TRUE) {
          updateDateRangeInput(session, 
                               "date",
                               "Date Range:",
                               start =  "2020-01-01", # set equal to  min 
                               end = Sys.Date(),  # set equal to  max 
                               min = "2020-01-01", 
                               max =Sys.Date())
      
        }
          
      })
      
      output$warning <- renderText({
        validate(
          need(input$date[2] > input$date[1], "Error in date specification")
        )
      })
    }
    
    shinyApp(ui = ui, server = server)
    
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-02
      • 1970-01-01
      • 2017-07-27
      • 2019-11-24
      • 1970-01-01
      • 2013-04-06
      • 1970-01-01
      相关资源
      最近更新 更多