How to use daterangeinput in r Shiny to filter by columns that have months and years by name?
Probably not the tightest code, but I got it to work.
Rather than treating each date as a string initially, I made them into a simple 1x1 tibble, so I could use case_when(), separate(), and then unite() the strings in the proper order.
library(shiny)
library(shinyjs)
library(shinydashboard)
library(shinyWidgets)
library(dplyr)
library(htmltools)
library(lubridate)
library(stringr)
ui = fluidPage(
tabsetPanel(
tabPanel("View 1", fluid = TRUE,
sidebarLayout(
sidebarPanel(
h4("Select Your Desired Filters"),
div(id = "inputs",
dateRangeInput(
inputId = "date_filter",
label = "Filter by Month and Year",
start = today(),
end = (today() + 90),
min = "Apr-2021",
max = NULL,
format = "M-yyyy",
startview = "month",
weekstart = 0,
language = "en",
separator = " to ",
width = NULL,
autoclose = TRUE
),
br()),
),
mainPanel(
DT::dataTableOutput("mytable")
)
)
)
)
)
server = function(input, output, session) {
#Here's the dataset
testdata <- tibble(employee = c("Justin", "Corey","Sibley"),
apr_2021 = c(10, 100, 101),
may_2021 = c(1, 4, 7),
jun_2021 = c(4, 5, 6),
jul_2021 = c(11, 11, 45),
aug_2021 = c(4, 5, 7),
sep_2021 = c(2, 1, 0),
oct_2021 = c(4, 5, 8),
nov_2021 = c(4, 1, 1))
select_values <- reactive({
from_date <- tibble(date = as.character(input$date_filter[1]))
from_date <- from_date %>%
mutate(date = str_remove_all(date, "-..$")) %>%
separate(date, into = c("year", "month"), sep = "-") %>%
mutate(month = case_when(
month == "01" ~ "jan",
month == "02" ~ "feb",
month == "03" ~ "mar",
month == "04" ~ "apr",
month == "05" ~ "may",
month == "06" ~ "jun",
month == "07" ~ "jul",
month == "08" ~ "aug",
month == "09" ~ "sep",
month == "10" ~ "oct",
month == "11" ~ "nov",
month == "12" ~ "dec",
TRUE~ "ERROR"
)) %>%
unite("month_year", c(month, year), sep = "_")
from_date <- parse_character(from_date$month_year)
to_date <- tibble(date = as.character(input$date_filter[2]))
to_date <- to_date %>%
mutate(date = str_remove_all(date, "-..$")) %>%
separate(date, into = c("year", "month"), sep = "-") %>%
mutate(month = case_when(
month == "01" ~ "jan",
month == "02" ~ "feb",
month == "03" ~ "mar",
month == "04" ~ "apr",
month == "05" ~ "may",
month == "06" ~ "jun",
month == "07" ~ "jul",
month == "08" ~ "aug",
month == "09" ~ "sep",
month == "10" ~ "oct",
month == "11" ~ "nov",
month == "12" ~ "dec",
TRUE~ "ERROR"
)) %>%
unite("month_year", c(month, year), sep = "_")
to_date <- parse_character(to_date$month_year)
testdata %>%
dplyr::select(employee, from_date:to_date)
})
output$mytable = DT::renderDataTable({
datatable(select_values())
})
}
shinyApp(ui = ui, server = server)
How to make only mondays selectable in daterangeinput in shiny R?
Unfortunately, there is no built-in feature of function dateRangeInput
. However, one can create a hook to evaluate if a given input is valid or not i.e. both start and end date is on a Monday:
library(shiny)
library(lubridate)
ui <- fluidPage(
dateRangeInput("daterange1", "Date range:",
start = "2001-01-01",
end = "2010-12-31"
),
textOutput("daterange1_valid")
)
server <- function(input, output, session) {
output$daterange1_valid <- renderText({
is_valid <- all(input$daterange1 %>% map_lgl(~ wday(.x, label = TRUE) == "Mon"))
ifelse(is_valid, "valid", "not valid. Start and end must be on a Monday!")
})
}
shinyApp(ui, server)
Another way is to just use two dateInput
elements instead. This will allow you to also color days other than Monday grey in the picker.
How to only select month and year in a shiny widget
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)
Shiny - years input only?
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
)
Change date format in R Shiny daterangeinput
You can use :
library(shiny)
ui <- fluidPage(
dateRangeInput(
inputId = "date_filter",
label = "Filter by Date",
start = "2020-01-01",
end = NULL,
min = "2020-01-01",
max = NULL,
format = "m-d-yyyy",
startview = "month",
weekstart = 0,
language = "en",
separator = " to ",
width = NULL,
autoclose = TRUE
),
textOutput('text')
)
shinyApp(ui, server = function(input, output) {
output$text <- renderText({
x <- format(input$date_filter, "%d/%m/%Y")
sprintf("You're viewing data from %s to %s", x[1], x[2])
})
})
Related Topics
Dplyr - Summary Table for Multiple Variables
How Can Put Multiple Plots Side-By-Side in Shiny R
Visualizing R Function Dependencies
Package Dependencies When Installing from Source in R
Using Legend with Stat_Function in Ggplot2
How to Create a Different Report for Each Subset of a Data Frame with R Markdown
Applying the Same Factor Levels to Multiple Variables in an R Data Frame
Multiple Histograms in Ggplot2
Why Is Expand.Grid Faster Than Data.Table 's Cj
Ggplot2: Have Shorter Tick Marks for Tick Marks Without Labels
Using Predict with a List of Lm() Objects
How to Transpose a Dataframe in Tidyverse
Get Map with Specified Boundary Coordinates
Are Recursive Functions Used in R
Using ':=' in Data.Table to Sum the Values of Two Columns in R, Ignoring Nas
Join Data.Table on Exact Date or If Not the Case on the Nearest Less Than Date