R 输入不包括minViewMode选项
我正在使用Shiny在R中开发一个应用程序,我想对我正在使用的dateInput使用minViewMode的datepicker选项 我已经查看了闪亮的官方文档,似乎dateInput小部件没有考虑这个选项。如何在R代码中使用此选项?这是我的ui.R代码:R 输入不包括minViewMode选项,r,datepicker,shiny,bootstrap-datepicker,R,Datepicker,Shiny,Bootstrap Datepicker,我正在使用Shiny在R中开发一个应用程序,我想对我正在使用的dateInput使用minViewMode的datepicker选项 我已经查看了闪亮的官方文档,似乎dateInput小部件没有考虑这个选项。如何在R代码中使用此选项?这是我的ui.R代码: dateInput("InitialDateGlobalViewTrafficSplit" ,h4("Initial date") ,value=as.Date(InitialDate)
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")
mydateInput也许更简洁的编码方式如下:
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
}
)这里回答了这个问题:
dateInputCustom("InitialDateGlobalViewTrafficSplit",
h4("Initial date"),
value = as.Date(InitialDate),
startDate = "2012-01-01",
weekstart = 1,
minViewMode = 1