SHARE
TWEET

Untitled

a guest Jun 16th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. library("shiny")
  2. library("ggplot2")
  3. library("DT")
  4. library("reshape2")
  5. ## load data ##
  6.  
  7. aqi = read.csv(file = "C:/Users/stan/Desktop/AIR/month.csv",
  8.                header = T, sep = ",")
  9. str(aqi)
  10. colnames(aqi) = c("num","month",
  11.                   "PM10","PM2.5","NO","NO2")
  12. ## ui.R ##
  13.  
  14. ui = fluidPage(
  15.   titlePanel('AIR'),
  16.   sidebarLayout(
  17.     sidebarPanel(
  18.       selectInput('month',
  19.                   'Choose a month:',
  20.                   c('All', unique(as.character(aqi$month)
  21.                   ))),
  22.       selectInput('PM10',
  23.                   'Number of :PM10',
  24.                   c('All', unique(as.character(aqi$PM10)
  25.                   ))),
  26.       selectInput('PM2.5',
  27.                   'Number of PM2.5:',
  28.                   c('All',unique(as.character(aqi$PM2.5)
  29.                   ))),
  30.       selectizeInput("AIR",
  31.                      "Select Contaminant:",
  32.                      choices = c( "PM2.5",
  33.                                   "PM10"),
  34.                      selected = "PM2..5",
  35.                      multiple = TRUE )
  36.   ),
  37.  
  38.     mainPanel(
  39.       dataTableOutput('table'),
  40.       plotOutput("plot")
  41.     )
  42.   )
  43. )
  44.  
  45.  
  46. ## server.R ##
  47.  
  48. server = function(input, output) {
  49.   # Filter data based on selections
  50.   output$table <- DT::renderDataTable(DT::datatable({
  51.     data <- aqi
  52.     if (input$month != "All") {
  53.       data <- data[aqi$month == input$month,]
  54.     }
  55.     if (input$PM10 != "All") {
  56.       data <- data[aqi$PM10 == input$PM10,]
  57.     }
  58.     data
  59.     if (input$PM2.5 != "All") {
  60.       data <- data[aqi$PM2.5 == input$PM2.5,]
  61.     }
  62.     data
  63.   }))
  64.   output$plot = renderPlot({
  65.     plot.data <- melt(aqi, id.vars= "month")
  66.     #not sure if input$cnt is a list or a vector
  67.     #may need to manipulate that before passing
  68.     plot.data <- plot.data[plot.data$variable %in% input$AIR, ]
  69.     ggplot(plot.data) +
  70.       geom_line(mapping = aes(x = month, y = value , colour =variable)) +
  71.       labs (x = "month", y = "value", title = "AIR") +
  72.       scale_colour_discrete(name = "AIR")
  73.   })
  74. }
  75.  
  76. shinyApp(ui, server)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top