Skip to content
Snippets Groups Projects
Commit 0f0e8f18 authored by Tom Rushby's avatar Tom Rushby
Browse files

Shiny adventures part 1 - GB grid intensity 1 & 2

parent 638aecb8
No related branches found
No related tags found
No related merge requests found
# Working through shiny tutorial, lesson 1
# https://shiny.rstudio.com/tutorial/written-tutorial/lesson1/
library(shiny)
library(httr)
library(jsonlite)
library(lubridate)
library(ggplot2)
# Define functions ----
getIntensity<- function(dateGet = input$dateToGet) {
response <- httr::GET(paste0("https://api.carbonintensity.org.uk/intensity/",dateGet,"T00:00Z","/fw24h"))
intensityPeriod <- jsonlite::fromJSON(rawToChar(response$content))
periodStart <- lubridate::ymd_hm(intensityPeriod$data$from)
periodEnd <- lubridate::ymd_hm(intensityPeriod$data$to)
periodData<- intensityPeriod$data$intensity
intensityDF <- cbind(periodStart,periodEnd,periodData)
# create time midpoint for plotting
intensityDF$periodMid <- intensityDF$periodStart + lubridate::minutes(15)
return(intensityDF)
}
# Define UI for app that draws a histogram ----
ui <- fluidPage(
# App title ----
titlePanel("GB Carbon Intensity forecast"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Slider for the number of bins ----
dateInput(inputId = "dateToGet",
label = "Select date for forecast:",
value = NULL,
min = lubridate::ymd(2020-01-01),
max = lubridate::today() + lubridate::days(3),
format = "yyyy-mm-dd",
startview = "month",
weekstart = 1,
language = "en")
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Plot ----
textOutput(outputId = "dateText"),
#plotOutput(outputId = "intensityPlot")
# Output: Histogram ----
plotOutput(outputId = "intensityPlot")
)
)
)
# Define server logic required to draw a histogram ----
server <- function(input, output) {
# Display date selected ----
output$dateText <- renderText({
paste0("Date selected: ",input$dateToGet)
})
# Get data and create data.frame
#DF <- reactive({getIntensity(dateGet = dateToGet)})
# Create plot
output$intensityPlot <- renderPlot({
plotCaption = paste0("Data from: https://api.carbonintensity.org.uk",
"\nDate selected: ",input$dateToGet)
ggplot(getIntensity(dateGet = input$dateToGet), aes(periodEnd)) +
geom_step(aes(y = forecast), direction = "vh") +
geom_col(aes(x = periodMid, y = actual), alpha = 0.8) +
labs(x = "Date and time",
y = expression("Intensity gCO"[2]/kWh),
caption = plotCaption) +
theme(legend.position = "bottom")
})
}
shinyApp(ui = ui, server = server)
# Working through shiny tutorial, lesson 1
# https://shiny.rstudio.com/tutorial/written-tutorial/lesson1/
# https://shiny.rstudio.com/tutorial/
# Uses code from woRflow/howTo/apiTutorial.md
library(shiny)
library(httr)
library(jsonlite)
library(lubridate)
library(ggplot2)
library(tidyr)
# Define functions ----
getIntensity<- function(dateGet = input$dateToGet, longDF = TRUE) {
response <- httr::GET(paste0("https://api.carbonintensity.org.uk/intensity/",dateGet,"T00:00Z","/fw24h"))
intensityPeriod <- jsonlite::fromJSON(rawToChar(response$content))
periodStart <- lubridate::ymd_hm(intensityPeriod$data$from)
periodEnd <- lubridate::ymd_hm(intensityPeriod$data$to)
periodData<- intensityPeriod$data$intensity
intensityDF <- cbind(periodStart,periodEnd,periodData)
# create time midpoint for plotting
intensityDF$periodMid <- intensityDF$periodStart + lubridate::minutes(15)
if(longDF){
# prepare data - tidyr
intensityDF <- tidyr::gather(intensityDF, key = "measure", value = "value", c(forecast,actual))
} # end if(longDF)
return(intensityDF)
}
# Define UI for app that draws a histogram ----
ui <- fluidPage(
# App title ----
titlePanel("GB Carbon Intensity forecast"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Slider for the number of bins ----
dateInput(inputId = "dateToGet",
label = "Select date for forecast:",
value = NULL,
min = lubridate::ymd(2020-01-01),
max = lubridate::today() + lubridate::days(3),
format = "yyyy-mm-dd",
startview = "month",
weekstart = 1,
language = "en")
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Plot ----
textOutput(outputId = "dateText"),
#plotOutput(outputId = "intensityPlot")
# Output: Histogram ----
plotOutput(outputId = "intensityPlot")
)
)
)
# Define server logic required to draw a histogram ----
server <- function(input, output) {
# Display date selected ----
output$dateText <- renderText({
paste0("Date selected: ",input$dateToGet)
})
# Get data and create data.frame
#DF <- reactive({getIntensity(dateGet = dateToGet)})
# Create plot
output$intensityPlot <- renderPlot({
plotCaption = paste0("Data from: https://api.carbonintensity.org.uk",
"\nDate selected: ",input$dateToGet)
ggplot(getIntensity(dateGet = input$dateToGet, longDF = TRUE), aes(periodMid,value, fill = measure)) +
geom_col(position = "identity", alpha = 0.6) +
labs(x = "Date and time",
y = expression("Intensity gCO"[2]/kWh),
fill = "Intensity",
caption = plotCaption) +
theme(legend.position = "bottom")
})
}
shinyApp(ui = ui, server = server)
# Working through shiny tutorial, lesson 1
# https://shiny.rstudio.com/tutorial/written-tutorial/lesson1/
library(shiny)
# Define UI for app that draws a histogram ----
ui <- fluidPage(
# App title ----
titlePanel("My first shiny app."),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Slider for the number of bins ----
sliderInput(inputId = "bins",
label = "Number of bins:",
min = 5,
max = 50,
value = 25)
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Histogram ----
plotOutput(outputId = "distPlot")
)
)
)
# Define server logic required to draw a histogram ----
server <- function(input, output) {
# Histogram of the Old Faithful Geyser Data ----
# with requested number of bins
# This expression that generates a histogram is wrapped in a call
# to renderPlot to indicate that:
#
# 1. It is "reactive" and therefore should be automatically
# re-executed when inputs (input$bins) change
# 2. Its output type is a plot
output$distPlot <- renderPlot({
x <- faithful$waiting
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = "#75AADB", border = "orange",
xlab = "Waiting time to next eruption (in mins)",
main = "Histogram of waiting times")
})
}
shinyApp(ui = ui, server = server)
\ No newline at end of file
library(shiny)
ui <- fluidPage(
# *Input() functions
# *Output() functions
)
server <- function(input, output) {}
shinyApp(ui = ui, server = server)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment