我正在使用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 :)
)
由于
答案 0 :(得分: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")
答案 1 :(得分: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
)