Display only months in dateRangeInput or dateInput for a shiny app [R programming]

前端 未结 4 1647
时光取名叫无心
时光取名叫无心 2020-12-08 05:35

I am using shiny for creating a web app. One of my plots uses only months of a particular year to generate the points in the plot.

I want the users to select only th

4条回答
  •  旧巷少年郎
    2020-12-08 06:36

    Here is a another method (with less code redundancy and hopefully simpler), contributed by a colleague. Instead of copying the shiny::dateInput function code, it is possible to add the min/max-view-mode part to the Shiny object afterwards. Then the old parameter 'startview' and the new 'minview'/'maxview' can be used as expected:

    dateInput2 <- function(inputId, label, minview = "days", maxview = "decades", ...) {
      d <- shiny::dateInput(inputId, label, ...)
      d$children[[2L]]$attribs[["data-date-min-view-mode"]] <- minview
      d$children[[2L]]$attribs[["data-date-max-view-mode"]] <- maxview
      d
    }
    
    dateRangeInput2 <- function(inputId, label, minview = "days", maxview = "decades", ...) {
      d <- shiny::dateRangeInput(inputId, label, ...)
      d$children[[2L]]$children[[1]]$attribs[["data-date-min-view-mode"]] <- minview
      d$children[[2L]]$children[[3]]$attribs[["data-date-min-view-mode"]] <- minview
      d$children[[2L]]$children[[1]]$attribs[["data-date-max-view-mode"]] <- maxview
      d$children[[2L]]$children[[3]]$attribs[["data-date-max-view-mode"]] <- maxview
      d
    }
    

    And here is a minimal Shiny example:

    library(shiny)
    shinyApp(
      ui = fluidPage(
        dateInput2("test1", "Year", startview = "year", minview = "months", maxview = "decades"),
        dateRangeInput2("test2", "Years", startview = "year", minview = "months", maxview = "decades")
      ),
      server = function(input, output, session) {}
    )
    

    Update:

    To address darKnight's question below, I extended the example and introduced a parameter for setting also the maximum selectable time resolution. For a complete list of possible parameters, please refer to:

    https://bootstrap-datepicker.readthedocs.io/en/latest/options.html

提交回复
热议问题