Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # WITH TYPE AS RADIOBUTTON
- library(shiny)
- library(ggplot2)
- library(dplyr)
- library(lubridate)
- library(tidyr)
- library(dygraphs)
- library(xts)
- library(zoo)
- # Read in the csv into a dataframe
- kai_data<-read.csv("shiny_kaibosh.csv", stringsAsFactors = FALSE, header = TRUE)
- kai_data$Month=factor(kai_data$Month)
- print(kai_data)
- ##User Interface
- ui <- fluidPage(
- br(),
- tags$img(src = "/kaibosh_logo_colour2_64770.png", width="300px"),
- br(), br(),
- sidebarLayout(
- sidebarPanel(
- uiOutput("unitOutput"),
- uiOutput("regionOutput"),
- uiOutput("typeOutput")
- ),
- mainPanel(
- dygraphOutput("dygraph"),
- # plotOutput("kaiboshplot"),
- br(), br(),
- tableOutput("results")
- )
- )
- )
- server <- function(input, output) {
- output$regionOutput <- renderUI({
- selectInput("regionInput", "Choose Region",
- unique(sort(kai_data$Region)),
- selected = "City")
- })
- output$unitOutput <- renderUI({
- radioButtons("unitInput", "Choose Unit",
- choices = c("KG",
- "Emissions",
- "Meals",
- "%"),
- selected = "KG")
- })
- output$typeOutput <- renderUI({
- checkboxGroupInput("typeInput", "Choose Food Type",
- unique(sort(kai_data$Type)))
- })
- filtered <- reactive({
- if (is.null(input$regionInput)) {
- return(NULL)
- }
- if (is.null(input$typeInput)) {
- return(NULL)
- }
- # Filter by selected region
- kai_data <- filter(kai_data, Region == input$regionInput)
- # Update the dataset based on unit input
- if (input$unitInput == "KG") {
- kai_data
- } else if (input$unitInput == "Emissions") {
- kai_data["Weight"] = kai_data["Weight"] / 0.35
- } else if (input$unitInput == "Meals") {
- kai_data["Weight"] = kai_data["Weight"] * 0.361
- } else if (input$unitInput == "%") {
- kai_data
- }
- #Find all the selected checkboxes
- target <- c(input$typeInput)
- # now we have a list of selected types so we filter the dataset to just these
- filtered_kai <- filter(kai_data, Type %in% target)
- # Now we split into separate dataframes to convert to time series -> now we have a list of dataframes
- split_kai <- split(filtered_kai, filtered_kai$Type, drop = FALSE)
- # Let's loop through convert to time series!
- kai_as_xts <- lapply(split_kai, function(kai) xts(kai, order.by = as.Date(as.yearmon(kai$Month, "%Y-%m"))))
- # use cbind on every element in the list
- final_data <- do.call(cbind, kai_as_xts)
- return (final_data)
- })
- output$dygraph <- renderDygraph({
- dygraph(filtered())
- })
- # output$results <- renderTable({
- # filtered()
- # })
- }
- ##final line of code
- shinyApp(ui = ui, server = server)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement