Skip to content

Instantly share code, notes, and snippets.

@OliverBears
Last active December 16, 2015 03:59
Show Gist options
  • Save OliverBears/5373430 to your computer and use it in GitHub Desktop.
Save OliverBears/5373430 to your computer and use it in GitHub Desktop.
This is user interface to display energy consumption and energy cost with time (Daily and Monthly). There are two dataframes generated in the global.R. Based on the input of time, a right dataframe should be chosen, either shinyday or shinymonth. After inputting the start day and end day by users, the server.R will do a comparison with the time …
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,
)
})
output$Plot <- reactivePlot(function() {
if (input$dataset==shinyday){
if (input$consumption) {
start1 = paste0(input$Year1, "-", input$Month1, "-", input$Day1)
start_energy = as.POSIXlt(start1)
in_start_energy <- start_energy
end1 = paste0(input$Year2, "-", input$Month2, "-", input$Day2)
end_energy = as.POSIXlt(end1)
in_end_energy <- end_energy
shinydayenergy = shinyday[(shinyday$time>=in_start_energy) & (shinyday$time<=in_end_energy),]
plot(shinydayenergy$time,shinydayenergy$energy)
}
if (input$cost) {
start2 = paste0(input$Year1, "-", input$Month1, "-", input$Day1)
start_cost = as.POSIXlt(start2)
in_start_cost <- start_cost
end2 = paste0(input$Year2, "-", input$Month2, "-", input$Day2)
end_cost = as.POSIXlt(end2)
in_end_cost <- end_cost
shinydaycost = shinyday[(shinyday$time>=in_start_cost) & (shinyday$time<=in_end_cost),]
plot(shinydaycost$time,shinydaycost$cost)
}
}
if (input$dataset==shinymonth){
if (input$consumption) {
start1 = paste0(input$Year1, "-", input$Month1, "-", input$Day1)
start_energy = as.POSIXlt(start1)
in_start_energy <- start_energy
end1 = paste0(input$Year2, "-", input$Month2, "-", input$Day2)
end_energy = as.POSIXlt(end1)
in_end_energy <- end_energy
shinymonthenergy = shinymonth[(shinymonth$time>=in_start_energy) & (shinymonth$time<=in_end_energy),]
plot(shinymonthenergy$time,shinymonthenergy$energy)
}
if (input$cost) {
start2 = paste0(input$Year1, "-", input$Month1, "-", input$Day1)
start_cost = as.POSIXlt(start2)
in_start_cost <- start_cost
end2 = paste0(input$Year2, "-", input$Month2, "-", input$Day2)
end_cost = as.POSIXlt(end2)
in_end_cost <- end_cost
shinymonthcost = shinymonth[(shinymonth$time>=in_start_cost) & (shinymonth$time<=in_end_cost),]
plot(shinymonthcost$time,shinymonthcost$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:",
list("2000", "2001","2002","2003", "2004","2005","2006", "2007","2008","2009", "2010","2011","2012", "2013"
)),
selectInput("Month1", "",
list("01", "02","03","04", "05","06","07", "08","09","10", "11","12"
)),
selectInput("Day1", "",
list("1", "2","3","4", "5","6", "7","8","9", "10","11", "12","13","14", "15","16", "17","18","19", "20","21", "22","23","24", "25","26", "27","28","29", "30","31"
)),
selectInput("Year2", "End Date:",
list("2000", "2001","2002","2003", "2004","2005","2006", "2007","2008","2009", "2010","2011","2012", "2013"
)),
selectInput("Month2", "",
list("01", "02","03","04", "05","06","07", "08","09","10", "11","12"
)),
selectInput("Day2", "",
list("1", "2","3","4", "5","6", "7","8","9", "10","11", "12","13","14", "15","16", "17","18","19", "20","21", "22","23","24", "25","26", "27","28","29", "30","31"
))
),
mainPanel(
plotOutput("Plot")
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment