【问题标题】:Shiny dateInput not including minViewMode option闪亮的 dateInput 不包括 minViewMode 选项
【发布时间】:2014-08-22 02:53:21
【问题描述】:

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

谢谢

【问题讨论】:

标签: r datepicker shiny bootstrap-datepicker


【解决方案1】:

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

 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")

【讨论】:

    【解决方案2】:

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

    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
    

    )

    【讨论】:

      猜你喜欢
      • 2019-01-09
      • 2015-05-16
      • 2014-12-14
      • 1970-01-01
      • 2014-07-06
      • 2019-09-27
      • 2018-04-03
      • 2021-06-12
      • 2014-01-21
      相关资源
      最近更新 更多