18

Is there a way to Hack or Create a dateRangeInput() selector in Shiny so that it selects only month-year (no day) or that it automatically selects the first day of the selected month without displaying a day choice ? Or should I create another month-date picker (sliders, selectbox...)

dateRangeInput('dateRange',label = "Pédiode d'analyse : ",format = "mm/yyyy",language="fr",
    start = Sys.Date() %m-% months(12), end=Sys.Date(),startview = "year",separator = " - ")

What i want is to delete this step when choosing the date : dateRangeInput

TiFr3D
  • 459
  • 1
  • 4
  • 11

2 Answers2

12

I've just used airDatePicker() to do this. You can edit the minimum view of the popup calendar to "month" and select the date format as "yyyy-mm".

airDatepickerInput("input_var_name",
                   label = "Start month",
                   value = "2015-10-01",
                   maxDate = "2016-08-01",
                   minDate = "2015-08-01",
                   view = "months", #editing what the popup calendar shows when it opens
                   minView = "months", #making it not possible to go down to a "days" view and pick the wrong date
                   dateFormat = "yyyy-mm"
                   )

The version I've just been working on looks like this (greyed may is where cursor was hovering when I took the screenshot):

example date picker showing only month

aclong
  • 181
  • 2
  • 7
9

Have a look at this custom function monthStart below which can be used to force the date to the first date of that month and year

Example 1, Display the first day of a given month. This may be useful if you want to use the date object for later use in your app, so it will always point to the first day of a given month and year

#rm(list=ls())
library(shiny)

monthStart <- function(x) {
  x <- as.POSIXlt(x)
  x$mday <- 1
  as.Date(x)
}

ui <- basicPage(dateRangeInput('dateRange',label = "Pédiode d'analyse : ",format = "mm/yyyy",language="fr",start = Sys.Date(), end=Sys.Date(),startview = "year",separator = " - "),
                textOutput("SliderText")
)
server <- shinyServer(function(input, output, session){

  Dates <- reactiveValues()
  observe({
    Dates$SelectedDates <- c(as.character(monthStart(input$dateRange[1])),as.character(monthStart(input$dateRange[2])))
  })
  output$SliderText <- renderText({Dates$SelectedDates})
})
shinyApp(ui = ui, server = server)

enter image description here

Example 2, Display only the month and year

#rm(list=ls())
library(shiny)

monthStart <- function(x) {
  x <- as.POSIXlt(x)
  x$mday <- 1
  as.Date(x)
}

ui <- basicPage(dateRangeInput('dateRange',label = "Pédiode d'analyse : ",format = "mm/yyyy",language="fr",start = Sys.Date(), end=Sys.Date(),startview = "year",separator = " - "),
                textOutput("SliderText")
)
server <- shinyServer(function(input, output, session){

  Dates <- reactiveValues()
  observe({
    Dates$SelectedDates <- c(as.character(format(input$dateRange[1],format = "%m/%Y")),as.character(format(input$dateRange[2],format = "%m/%Y")))
  })
  output$SliderText <- renderText({Dates$SelectedDates})
})
shinyApp(ui = ui, server = server)

enter image description here

Pork Chop
  • 28,528
  • 5
  • 63
  • 77
  • Ok, but I don't want that the user think that a day has been choosen, may be the best way to achieve that is to create a slider or a selectinput with mm-yyyy. – TiFr3D Dec 07 '16 at 19:56
  • As I didn't know how you want to use this feature I included the day also, as in your analysis you might need it. For display purposes, you can simply render the date without the day. See updates above – Pork Chop Dec 08 '16 at 08:07
  • Sorry i didn't explain it clearly, i've added a picture to show the step that i want to delete or hide. – TiFr3D Dec 08 '16 at 21:21
  • in this case u should use a custom made text input – Pork Chop Dec 08 '16 at 21:40