Display only months in dateRangeInput or dateInput for a shiny app [R programming]

前端 未结 4 1630
时光取名叫无心
时光取名叫无心 2020-12-08 05:35

I am using shiny for creating a web app. One of my plots uses only months of a particular year to generate the points in the plot.

I want the users to select only th

4条回答
  •  有刺的猬
    2020-12-08 06:24

    @MartinJohnHadley: Basically by adding the same three lines @StevenMortimer added to dateInput's code to dateRangeInput. This adds the minViewMode to shinys dateRangeInput.

    1. Find the code at https://github.com/rstudio/shiny/blob/master/R/input-daterange.R
    2. add default argument minviewmode="months"
    3. add data-date-min-view-mode = minviewmode to both divs
    4. Add missing arguments (search in github archive of shiny)
    5. Enjoy your custom dateRange input :-)

    Best regards, sandro

    Code:

    dateRangeMonthsInput <- function(inputId, label, start = NULL, end = NULL,
                                min = NULL, max = NULL, format = "yyyy-mm-dd", startview = "month",
                                minviewmode="months", # added manually
                                weekstart = 0, language = "en", separator = " to ", width = NULL) {
    
       # If start and end are date objects, convert to a string with yyyy-mm-dd format
       # Same for min and max
       if (inherits(start, "Date"))  start <- format(start, "%Y-%m-%d")
       if (inherits(end,   "Date"))  end   <- format(end,   "%Y-%m-%d")
       if (inherits(min,   "Date"))  min   <- format(min,   "%Y-%m-%d")
       if (inherits(max,   "Date"))  max   <- format(max,   "%Y-%m-%d")
    
       htmltools::attachDependencies(
         div(id = inputId,
             class = "shiny-date-range-input form-group shiny-input-container",
             style = if (!is.null(width)) paste0("width: ", validateCssUnit(width), ";"),
    
             controlLabel(inputId, label),
             # input-daterange class is needed for dropdown behavior
             div(class = "input-daterange input-group",
                 tags$input(
                   class = "input-sm form-control",
                   type = "text",
                   `data-date-language` = language,
                   `data-date-weekstart` = weekstart,
                   `data-date-format` = format,
                   `data-date-start-view` = startview,
                   `data-date-min-view-mode` = minviewmode, # added manually
                   `data-min-date` = min,
                   `data-max-date` = max,
                   `data-initial-date` = start
                 ),
                 span(class = "input-group-addon", separator),
                 tags$input(
                   class = "input-sm form-control",
                   type = "text",
                   `data-date-language` = language,
                   `data-date-weekstart` = weekstart,
                   `data-date-format` = format,
                   `data-date-start-view` = startview,
                   `data-date-min-view-mode` = minviewmode, # added manually
                   `data-min-date` = min,
                   `data-max-date` = max,
                   `data-initial-date` = end
                 )
             )
         ),
         datePickerDependency
       )
     }
    
     `%AND%` <- function(x, y) {
       if (!is.null(x) && !is.na(x))
         if (!is.null(y) && !is.na(y))
           return(y)
       return(NULL)
     }
    
     controlLabel <- function(controlName, label) {
       label %AND% tags$label(class = "control-label", `for` = controlName, label)
     }
    
     # the datePickerDependency is taken from https://github.com/rstudio/shiny/blob/master/R/input-date.R
     datePickerDependency <- htmltools::htmlDependency(
     "bootstrap-datepicker", "1.6.4", c(href = "shared/datepicker"),
     script = "js/bootstrap-datepicker.min.js",
     stylesheet = "css/bootstrap-datepicker3.min.css",
     # Need to enable noConflict mode. See #1346.
     head = "")
    

提交回复
热议问题