我正在使用Shiny在R中开发应用程序,我想将minViewMode的datepicker选项用于我正在使用的dateInput。

我已经检查了Shiny的官方文档,并且似乎没有为dateInput小部件考虑此选项。如何在我的R代码中使用此选项?这是我的ui.R代码:

dateInput("InitialDateGlobalViewTrafficSplit"
          ,h4("Initial date")
          ,value=as.Date(InitialDate)
          ,min = ("2012-01-01")
          ,weekstart = 1
          # ,minViewMode = 1 # Ideal solution :)
          )

谢谢

最佳答案

尝试定义此自定义输入,并将其与建议的调用一起使用:

 mydateInput <- function(inputId, label, value = NULL, min = NULL, max = NULL,
                  format = "yyyy-mm-dd", startview = "month", weekstart = 0, language = "en", minviewmode="months",
                       width = NULL) {

   # If value is a date object, convert it to a string with yyyy-mm-dd format
   # Same for min and max
   if (inherits(value, "Date"))  value <- format(value, "%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(
     tags$div(id = inputId,
              class = "shiny-date-input form-group shiny-input-container",
              style = if (!is.null(width)) paste0("width: ", validateCssUnit(width), ";"),

              controlLabel(inputId, label),
              tags$input(type = "text",
                         # datepicker class necessary for dropdown to display correctly
                         class = "form-control datepicker",
                         `data-date-language` = language,
                         `data-date-weekstart` = weekstart,
                         `data-date-format` = format,
                         `data-date-start-view` = startview,
                         `data-date-min-view-mode` = minviewmode,
                         `data-min-date` = min,
                         `data-max-date` = max,
                         `data-initial-date` = value
              )
     ),
     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)
 }

 datePickerDependency <- htmlDependency(
   "bootstrap-datepicker", "1.0.2", c(href = "shared/datepicker"),
   script = "js/bootstrap-datepicker.min.js",
   stylesheet = "css/datepicker.css")

关于r - Shiny 的dateInput不包含minViewMode选项,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24512077/

10-16 11:59