Home > Net >  How to only select month and year in a shiny widget
How to only select month and year in a shiny widget

Time:02-21

My normal Shiny date widget offers date selection by year-month-day. REPREX:

# Load library

library(shiny)
library(dplyr)

# Load data & prepare data

data(economics)

dat <- economics %>% filter(date > '2014-01-01')


# Define UI 

ui <- fluidPage(
     
     dateRangeInput(
          
          inputId = 'enter_dt',
          label = 'select timeframe:',
          start = max(dat$date),
          end = min(dat$date) ,

     )
)

# Define server 

server <- function(input, output) {
     
    
}

# Run the application 

shinyApp(ui = ui, server = server)

Above code gives me this:

enter image description here

I am trying to get a widget which only offers year-month like this:

enter image description here

Please advise.

CodePudding user response:

We could use airDatepickerInput from shinyWidgets:

library(shiny)
library(dplyr)
library(shinyWidgets)

# Load data & prepare data
data(economics)
dat <- economics %>% filter(date > '2014-01-01')


# Define UI 

ui <- fluidPage(
  
  airDatepickerInput("input_var_name",
                     label = "Start month",
                     value = "2022-10-01",
                     maxDate = "2022-12-01",
                     minDate = "2022-01-01",
                     view = "months", 
                     minView = "months", 
                     dateFormat = "yyyy-mm"
  )
)

# Define server 

server <- function(input, output) {
  
  
}

# Run the application 

shinyApp(ui = ui, server = server)

enter image description here

  • Related