Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.85 KB | None | 0 0
  1. # WITH TYPE AS RADIOBUTTON
  2.  
  3. library(shiny)
  4. library(ggplot2)
  5. library(dplyr)
  6. library(lubridate)
  7. library(tidyr)
  8. library(dygraphs)
  9. library(xts)
  10. library(zoo)
  11.  
  12. # Read in the csv into a dataframe
  13. kai_data<-read.csv("shiny_kaibosh.csv", stringsAsFactors = FALSE, header = TRUE)
  14. kai_data$Month=factor(kai_data$Month)
  15.  
  16. print(kai_data)
  17.  
  18. ##User Interface
  19. ui <- fluidPage(
  20.  
  21.   br(),
  22.   tags$img(src = "/kaibosh_logo_colour2_64770.png", width="300px"),
  23.   br(), br(),
  24.  
  25.   sidebarLayout(
  26.     sidebarPanel(
  27.       uiOutput("unitOutput"),
  28.       uiOutput("regionOutput"),
  29.       uiOutput("typeOutput")
  30.     ),
  31.     mainPanel(
  32.       dygraphOutput("dygraph"),
  33.       # plotOutput("kaiboshplot"),
  34.       br(), br(),
  35.       tableOutput("results")
  36.     )
  37.   )
  38. )
  39.  
  40. server <- function(input, output) {
  41.   output$regionOutput <- renderUI({
  42.     selectInput("regionInput", "Choose Region",
  43.                 unique(sort(kai_data$Region)),
  44.                 selected = "City")
  45.   })
  46.  
  47.   output$unitOutput <- renderUI({
  48.     radioButtons("unitInput", "Choose Unit",
  49.                    choices = c("KG",
  50.                                "Emissions",
  51.                                "Meals",
  52.                                "%"),
  53.                     selected = "KG")
  54.   })
  55.  
  56.   output$typeOutput <- renderUI({
  57.     checkboxGroupInput("typeInput", "Choose Food Type",
  58.                 unique(sort(kai_data$Type)))
  59.   })
  60.  
  61.   filtered <- reactive({
  62.     if (is.null(input$regionInput)) {
  63.       return(NULL)
  64.     }
  65.     if (is.null(input$typeInput)) {
  66.       return(NULL)
  67.     }
  68.  
  69.     # Filter by selected region
  70.     kai_data <- filter(kai_data, Region == input$regionInput)
  71.     # Update the dataset based on unit input
  72.     if (input$unitInput == "KG") {
  73.       kai_data
  74.     } else if (input$unitInput == "Emissions") {
  75.       kai_data["Weight"] = kai_data["Weight"] / 0.35
  76.     } else if (input$unitInput == "Meals") {
  77.       kai_data["Weight"] = kai_data["Weight"] * 0.361
  78.     } else if (input$unitInput == "%") {
  79.       kai_data
  80.     }
  81.  
  82.     #Find all the selected checkboxes
  83.     target <- c(input$typeInput)
  84.     # now we have a list of selected types so we filter the dataset to just these
  85.     filtered_kai <- filter(kai_data, Type %in% target)
  86.     # Now we split into separate dataframes to convert to time series -> now we have a list of dataframes
  87.     split_kai <- split(filtered_kai, filtered_kai$Type, drop = FALSE)
  88.     # Let's loop through convert to time series!
  89.     kai_as_xts <- lapply(split_kai, function(kai) xts(kai, order.by = as.Date(as.yearmon(kai$Month, "%Y-%m"))))
  90.     # use cbind on every element in the list
  91.     final_data <- do.call(cbind, kai_as_xts)
  92.     return (final_data)
  93.  
  94.   })
  95.  
  96.   output$dygraph <- renderDygraph({
  97.     dygraph(filtered())
  98.   })
  99.  
  100.   # output$results <- renderTable({
  101.   #   filtered()
  102.   # })
  103. }
  104.  
  105. ##final line of code
  106. shinyApp(ui = ui, server = server)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement