Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(shiny)
- library(shinydashboard)
- library(tidyverse)
- random_data <- data.frame(replicate(2, sample(0:10, 1000, rep=TRUE)))
- set.seed(1984)
- random_data$date <- sample(seq(as.Date('2016-01-01'), as.Date(Sys.Date()), by = "day"), 1000)
- sidebar <- dashboardSidebar(dateRangeInput(
- "dates", label = h4("Date range"), start = '2016-01-01', end = Sys.Date(),
- format = "dd-mm-yyyy", startview = "year", min = "2016-01-01", max = Sys.Date()
- ))
- body <- dashboardBody(
- textOutput("selected_dates"),
- br(),
- fluidRow(
- infoBoxOutput("total", width = 12)
- ),
- fluidRow(
- box(width = 12, solidHeader = TRUE,
- title = "X1 over time",
- plotOutput(outputId = "x1_time")
- )
- ),
- fluidRow(
- box(width = 12, solidHeader = TRUE,
- title = "X2 over time",
- plotOutput(outputId = "x2_time")
- )
- )
- )
- ui <- dashboardPage(dashboardHeader(title = "Example"),
- sidebar,
- body
- )
- server <- function(input, output) {
- filtered <- reactive({
- filtered_data <- random_data %>%
- filter(date >= input$dates[1] & date <= input$dates[2])
- return(filtered_data)
- })
- output$selected_dates <- renderText({
- validate(
- need(input$dates[2] >= input$dates[1], "End date is earlier than start date"
- )
- )
- })
- output$total<- renderInfoBox({
- validate(
- need(input$dates[2] >= input$dates[1], "")
- )
- infoBox(title = "Sample size",
- value = nrow(filtered()),
- icon = icon("binoculars"), color = "light-blue")
- })
- output$x1_time <- renderPlot({
- validate(
- need(input$dates[2] >= input$dates[1], "")
- )
- x1_time_plot <- ggplot(filtered(), aes(x = date, y = X1)) +
- geom_bar(stat = "identity")
- theme_minimal()
- x1_time_plot
- })
- output$x2_time <- renderPlot({
- validate(
- need(input$dates[2] >= input$dates[1], "")
- )
- x2_time_plot <- ggplot(filtered(), aes(x = date, y = X2)) +
- geom_bar(stat = "identity")
- theme_minimal()
- x2_time_plot
- })
- }
- shinyApp(ui, server)
Add Comment
Please, Sign In to add comment