3

我正在使用 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 :)
          )

谢谢

4

2 回答 2

0

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

 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")
于 2017-06-06T13:05:29.277 回答
0

也许更简洁的编码方式是这样的:

dateInputCustom <- function(inputId, label, minViewMode = 1, maxViewMode = 2, startDate = NULL, ...) { # set default values
  cal <- shiny::dateInput(inputId, label, format = "yyyy-mm-dd", ...)
  cal$children[[2L]]$attribs[["data-date-min-view-mode"]] <- minViewMode
  cal$children[[2L]]$attribs[["data-date-max-view-mode"]] <- maxViewMode
  cal$children[[2L]]$attribs[["data-date-start-date"]]    <- startDate
  cal
}

请注意,如果使用 minViewMode,则默认的 min 参数不起作用,因此您还必须实现 startDate 参数。

称它为 R Shiny:

dateInputCustom("InitialDateGlobalViewTrafficSplit",
      h4("Initial date"),
      value = as.Date(InitialDate),
      startDate = "2012-01-01",
      weekstart = 1,
      minViewMode = 1

)

于 2020-01-31T12:57:16.987 回答