Skip to content

Instantly share code, notes, and snippets.

@jcheng5
Forked from OliverBears/Global.R
Last active December 16, 2015 09:58
Show Gist options
  • Save jcheng5/5416530 to your computer and use it in GitHub Desktop.
Save jcheng5/5416530 to your computer and use it in GitHub Desktop.
library(chron)
library(shiny)
energy1=c(76.1,77,78.1,78.2,78.8,79.7,79.9,81.1,81.2,81.8,82.8,83.5)
cost1=c(7.1,7,7.1,7.2,7.8,7.7,7.9,8.1,8.2,8.8,8.8,8.5)
time1=c(20130301,20130401,20130501,20130601,20130701,20130801,20130901,20131001,20131101,20131201,20140101,20140201)
time1=as.Date(as.character(time1),format="%Y%m%d")
shinymonth=data.frame(time=time1,energy=energy1,cost=cost1)
energy2=rnorm(365,76.1,0.2)
cost2=rnorm(365,7.1,0.2)
time2=c(20130101:20130131,20130201:20130228,20130301:20130331,20130401:20130430,20130501:20130531,20130601:20130630,20130701:20130731,20130801:20130831,20130901:20130930,20131001:20131031,20131101:20131130,20131201:20131231)
time2=as.Date(as.character(time2),format="%Y%m%d")
shinyday=data.frame(time=time2,energy=energy2,cost=cost2)
library(shiny)
shinyServer(function(input, output) {
datasetInput <- reactive({
switch(input$dataset,
"Daily" = shinyday,
"Monthly" =shinymonth,
)
})
selectedData <- reactive({
start <- as.Date(as.POSIXlt(paste0(input$Year1, "-", input$Month1, "-", input$Day1)))
end <- as.Date(as.POSIXlt(paste0(input$Year2, "-", input$Month2, "-", input$Day2)))
data <- datasetInput()
data[data$time >= start && data$time <= end,]
})
output$ConsumptionPlot <- renderPlot({
if (nrow(selectedData()) == 0)
stop('No data selected')
plot(selectedData()$time, selectedData()$energy)
})
output$CostPlot <- renderPlot({
if (nrow(selectedData()) == 0)
stop('No data selected')
plot(selectedData()$time, selectedData()$cost)
})
})
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("Industrial Energy Management"),
sidebarPanel(
checkboxInput(inputId = "consumption",
label = strong("Energy Consumption"),
value = FALSE),
checkboxInput(inputId = "cost",
label = strong("Energy Cost"),
value = FALSE),
selectInput("dataset", "Time:",
choices = c("Daily","Monthly")),
selectInput("Year1", "Start Date:",
2000:2013),
selectInput("Month1", "",
1:12),
selectInput("Day1", "",
1:31),
selectInput("Year2", "End Date:",
2000:2013),
selectInput("Month2", "",
1:12),
selectInput("Day2", "",
1:31)
),
mainPanel(
conditionalPanel(
condition="input.consumption",
h3("Energy Consumption"),
plotOutput("ConsumptionPlot")
),
conditionalPanel(
condition="input.cost",
h3("Energy Cost"),
plotOutput("CostPlot")
)
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment