2
votes

How can I have year input only?

I have this date input below which gives me dates from 2014-10-01,

 dateInput(
    inputId =  "date_from", 
    label = "Select time period:", 
    value = "2014-10-01", 
    format = "dd-mm-yyyy"
    )

But I don't need the months and dates, only years that I need,

 dateInput(
    inputId =  "year_from", 
    label = "Select time period:", 
    value = "2014", 
    format = "yyyy"
    )

It does not work obviously as I still see the months and dates being displayed.

Any ideas?

2

2 Answers

7
votes

If you don't need months and days, then I'd argue that what you want is not dates but just year. If you just want year, then I would use a selectInput instead of a dateInput. Usually dateInput are used when you literally want a full date, but it makes a lot more sense to just show a select box with years if that's all you need. It's also a lot easier for the user to use from a user-experience point of view.

selectInput(
    inputId =  "date_from", 
    label = "Select time period:", 
    choices = 2014:2100
)
3
votes

With reference to Dean Attali answer, we can update the code as below to show the years till our current year only.

selectInput(
    inputId =  "date_from", 
    label = "Select time period:", 
    choices = 2014:as.numeric(format(Sys.Date(),"%Y"))
  )