Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
W
woRkflow
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Tom Rushby
woRkflow
Commits
91bab036
Commit
91bab036
authored
4 years ago
by
Tom Rushby
Browse files
Options
Downloads
Patches
Plain Diff
create copy and deploy app to shinyapps.io
parent
e1c3abc1
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
howTo/shiny/App/app.R
+129
-0
129 additions, 0 deletions
howTo/shiny/App/app.R
howTo/shiny/App/rsconnect/shinyapps.io/rushby/gridforecast.dcf
+10
-0
10 additions, 0 deletions
.../shiny/App/rsconnect/shinyapps.io/rushby/gridforecast.dcf
with
139 additions
and
0 deletions
howTo/shiny/App/app.R
0 → 100644
+
129
−
0
View file @
91bab036
# 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
)
intensityDF
<-
intensityDF
[
-1
,]
# remove 1st row
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 Grid Electricity Carbon Intensity Forecast"
),
tags
$
p
(
"This app was created by "
,
tags
$
a
(
href
=
"https://energy.soton.ac.uk/rushby/"
,
"Tom Rushby "
),
tags
$
a
(
href
=
"https://twitter.com/tom_rushby"
,
"(@tom_rushby)"
),
"for the "
,
tags
$
a
(
href
=
"https://energy.soton.ac.uk/"
,
"Energy and Climate Change Division"
),
tags
$
a
(
href
=
"https://twitter.com/EnergySoton"
,
"(@EnergySoton)"
),
"at the University of Southampton."
),
# Sidebar layout with input and output definitions ----
sidebarLayout
(
# Sidebar panel for inputs ----
sidebarPanel
(
fluidRow
(
tags
$
p
(
"Select the date to view by clicking in the box below (defaults to today's date)."
),
tags
$
p
(
"Data for forecast carbon intensity is available up to four days ahead."
),
tags
$
hr
()
),
# 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
(
fluidRow
(
tags
$
br
(),
tags
$
p
(
"The chart below shows the forecast and actual carbon intensity of electricity supplied by National Grid over the 24 hours selected."
,
"The data illustrated by this app is retrieved from "
,
tags
$
a
(
href
=
"https://carbonintensity.org.uk/"
,
"National Grid ESO's Carbon Intensity API"
),
"."
),
tags
$
p
(
"Predicted carbon intensity of electricity is shown in blue (forecast)."
,
"The carbon intensity of electricity supplied is shown in red (actual)."
),
tags
$
hr
(),
# Output: Text ----
#textOutput(outputId = "dateText"),
# Output: Histogram ----
plotOutput
(
outputId
=
"intensityPlot"
)
)
# row
)
# panel
)
# layout
)
# page
# 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"
,
"\nVisualisation: rushby.shinyapps.io/gridforecast"
)
plotTitle
=
paste0
(
"24-hour GB grid carbon intensity forecast for "
,
input
$
dateToGet
)
ggplot
(
getIntensity
(
dateGet
=
input
$
dateToGet
,
longDF
=
TRUE
),
aes
(
periodMid
,
value
,
fill
=
measure
))
+
geom_col
(
position
=
"identity"
,
alpha
=
0.6
)
+
expand_limits
(
y
=
c
(
0
,
350
))
+
labs
(
x
=
"Date and time"
,
y
=
expression
(
"Intensity gCO"
[
2
]
/
kWh
),
fill
=
"Intensity"
,
title
=
plotTitle
,
caption
=
plotCaption
)
+
theme
(
legend.position
=
"bottom"
)
})
}
shinyApp
(
ui
=
ui
,
server
=
server
)
This diff is collapsed.
Click to expand it.
howTo/shiny/App/rsconnect/shinyapps.io/rushby/gridforecast.dcf
0 → 100644
+
10
−
0
View file @
91bab036
name: gridforecast
title: GB Grid Electricity Carbon Intensity Forecast Visualisation
username:
account: rushby
server: shinyapps.io
hostUrl: https://api.shinyapps.io/v1
appId: 2811298
bundleId: 3630929
url: https://rushby.shinyapps.io/gridforecast/
when: 1600167776.24198
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment