Guest User

Untitled

a guest
Dec 11th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. library(shiny)
  2. library(shinydashboard)
  3. library(tidyverse)
  4.  
  5.  
  6. random_data <- data.frame(replicate(2, sample(0:10, 1000, rep=TRUE)))
  7. set.seed(1984)
  8. random_data$date <- sample(seq(as.Date('2016-01-01'), as.Date(Sys.Date()), by = "day"), 1000)
  9.  
  10. sidebar <- dashboardSidebar(dateRangeInput(
  11. "dates", label = h4("Date range"), start = '2016-01-01', end = Sys.Date(),
  12. format = "dd-mm-yyyy", startview = "year", min = "2016-01-01", max = Sys.Date()
  13. ))
  14.  
  15. body <- dashboardBody(
  16. textOutput("selected_dates"),
  17. br(),
  18. fluidRow(
  19. infoBoxOutput("total", width = 12)
  20. ),
  21. fluidRow(
  22. box(width = 12, solidHeader = TRUE,
  23. title = "X1 over time",
  24. plotOutput(outputId = "x1_time")
  25. )
  26. ),
  27. fluidRow(
  28. box(width = 12, solidHeader = TRUE,
  29. title = "X2 over time",
  30. plotOutput(outputId = "x2_time")
  31. )
  32. )
  33. )
  34.  
  35. ui <- dashboardPage(dashboardHeader(title = "Example"),
  36. sidebar,
  37. body
  38. )
  39.  
  40. server <- function(input, output) {
  41. filtered <- reactive({
  42. filtered_data <- random_data %>%
  43. filter(date >= input$dates[1] & date <= input$dates[2])
  44. return(filtered_data)
  45. })
  46.  
  47. output$selected_dates <- renderText({
  48. validate(
  49. need(input$dates[2] >= input$dates[1], "End date is earlier than start date"
  50. )
  51. )
  52. })
  53.  
  54.  
  55. output$total<- renderInfoBox({
  56. validate(
  57. need(input$dates[2] >= input$dates[1], "")
  58. )
  59. infoBox(title = "Sample size",
  60. value = nrow(filtered()),
  61. icon = icon("binoculars"), color = "light-blue")
  62. })
  63.  
  64. output$x1_time <- renderPlot({
  65. validate(
  66. need(input$dates[2] >= input$dates[1], "")
  67. )
  68. x1_time_plot <- ggplot(filtered(), aes(x = date, y = X1)) +
  69. geom_bar(stat = "identity")
  70. theme_minimal()
  71. x1_time_plot
  72. })
  73.  
  74. output$x2_time <- renderPlot({
  75. validate(
  76. need(input$dates[2] >= input$dates[1], "")
  77. )
  78. x2_time_plot <- ggplot(filtered(), aes(x = date, y = X2)) +
  79. geom_bar(stat = "identity")
  80. theme_minimal()
  81. x2_time_plot
  82. })
  83.  
  84. }
  85.  
  86. shinyApp(ui, server)
Add Comment
Please, Sign In to add comment