SlideShare a Scribd company logo
1 of 73
Download to read offline
Personalisation
One dataset > 150 webpages with one click
Who are we?
…
https://github.com/BBC-Data-Unit
What we’ll cover
Why we generated 150-page websites for partners
How to do it: parameterisation + GitHub Pages
Tips and tricks
Briefing document
sduiopc.github.io/test1
senspeech.github.io/website/
senspeech.github.io/website/Liverpool.html
The steps:
● Produce a ‘template’ analysis for a given area
● Produce an index (home) page
● Render multiple .md versions of that analysis -
one for each region
● Render HTML versions
● Publish as a website online (GitHub Pages)
● Debug, clean and tweak the code
Go to this URL
github.com/paulbradshaw/
parameterisation
and: https://drive.google.com/drive/folders/17WtozAfB1frGZmLOJbSbJooP1WRDIfY1?usp=sharing
The principles:
● Create a parameter — this should be what you
want to generate a different page for (e.g.
region, police force, category, politician)
● Limit code output to what should be in the
HTML file (visible elements)
● Loop through list and call a ‘knitting’ function to
create a file for each
R Markdown files:
a quick introduction
Create an R Markdown file
Give a title - and ✅ HTML
Knit - and title it again
Knit the file — you’ll be asked to name it
again
Markdown will generate HTML:
● Headings: # or ## or ###
● Italic, bold: * or ** or *** before and after
● Links: [link text](http://url)
● Indented text: >
● Bullet list items: *
● Code blocks: ```{r} above and ``` below
● Inline code: ` before and after
Notebook 1: the template
The YAML header
---
title: "01template"
author: "BIGNEWS"
date: "2023-05-25"
output: html_document
---
✅ Add a parameter
---
title: "01template"
author: "BIGNEWS"
date: "2023-05-25"
output: html_document
params:
reg: "Anyville"
---
✅ Change the title
---
title: |
Regional analysis: `r params$reg`
author: "BIGNEWS"
date: "2023-05-25"
output: html_document
params:
reg: "Anyville"
---
---
title: |
Regional analysis: `r params$reg`
author: "BIGNEWS"
date: "2023-05-25"
output: html_document
params:
reg: "Anyville"
---
What have we added?
Within that list an element called ‘reg’
with a value of ‘Anyville’ is created. We set
it to that value for the purpose of testing.
We define a list variable called ‘params’
The title fetches the ‘params’ list and
then drills down into the ‘reg’ element
within that, and fetches its value
Knit to see how it’s
changed!
---
title: |
Regional analysis: `r params$reg`
output: html_document
params:
reg: "Anyville"
---
Delete unwanted info
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Analysing stop and search
Police have been given new powers to stop and
search potential criminals - but are the
existing powers effective - and used fairly?
Find out how the numbers changed in
Add your own Markdown
Level 1
header
(1 hash, then
space)
Code block
with knit
settings (leave
for now)
# Analysing stop and search
Police have been given new powers to
stop and search potential criminals -
but are the existing powers effective
- and used fairly? Find out how the
numbers changed in `r params$reg`.
Use the parameter
Inline R code:
‘calls’ the variable
params$reg
Code blocks:
```{r}
Code goes here
#add comments to explain
```
● Code blocks and any messages/warnings will
be visible in the HTML version — unless you
specify otherwise in the Knitr settings
Code: import packages
```{r import packages}
#import the tidyverse
library(tidyverse)
#The datatables package
library(DT)
```
Another code block -
we’ve named it ‘import
packages’
Code to import
two packages -
with comments
Run the code!
Press play!
Another code
block
```{r import data}
#store the URL
fileloc =
"https://docs.google.com/spreadsheets/d/e/2PACX-
1vR2-w4JWtqaDmOBp7CxZpMaqsa-2Tm4vUVslOf5_NVfiTIa
vYEJWHWOv-4sGJ9VUUlx8eJPnULECeYW/pub?gid=0&singl
e=true&output=csv"
#Tidyverse's read_csv function imports from it
data = read_csv(fileloc)
```
Import the data
Store the
URL of the
CSV
Read the CSV
into a
dataframe
called ‘data’
When knitted…
Add two
parameters to
suppress
messages &
warnings
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE,
warning = F,
message = F)
```
# Analysing stop and search
Police have been given new powers to stop and
search potential criminals - but are the
Change Knitr settings
Change echo=
to FALSE (or F)
to hide code
from output
```{r filter and subset}
#filter to those for this la, and specified cols
subsetdf <- data %>%
dplyr::filter(region == params$reg)
``` Parameter
must match
value in data to
act as filter
Use parameter to filter
Extract figures to use
```{r extract figures}
#store the figures for the earliest and latest years
fig21 <- subsetdf$stops_2021
fig22 <- subsetdf$stops_2022
#calculate the change in this region
localchange <- (fig22-fig21)/fig21
#calculate the change across all regions
nationalchange <-
(sum(data$stops_2022)-sum(data$stops_2021))/sum(data$stops_
2021)
```
```{r create custom string}
#set a variable to 'rose' or 'dropped' depending on relation of
earlier figure to newer one
changetype <- ifelse(isTRUE(fig22 > fig21), "rose", "dropped")
#create a custom string with that
customstring1 <- paste0("In **",params$reg,"** the number of stops
",changetype," from **",fig21,"** in 2021 to **",fig22,"** in
2022.")
```
`r customstring1`
Code block: creates a text string, inserting
variables for particular region
Template text + variables
Inline code: ‘calls’ that string variable
```{r create another string}
nationalcomparison <- ifelse(localchange>nationalchange, "higher
than", "below the")
#create a custom string with that
customstring2 <- paste0("The number of people being stopped in 2022
**",changetype," by ",round(localchange*100,1),"%** compared to the
previous year. This is ",nationalcomparison," the national rise of
**",round(nationalchange*100, 1),"%**.")
```
`r customstring2`
Code block: creates a text string, inserting
variables for particular region
Another line…
Inline code: ‘calls’ that string variable
That custom string broken down
paste0(
"The number of people being stopped in 2022
**",
changetype,
" by ",
round(localchange*100,1),
"%** compared to the previous year. This is ",
nationalcomparison,
" the national rise of **",
round(nationalchange*100, 1),
"%**.")
## Explore your area
```{r table}
#Create the datatable. Add a caption if you want
DT::datatable(data,
style = 'bootstrap',
caption = 'Numbers of people being stopped and searched',
filter = 'top',
options = list(pageLength = 10, scrollX=TRUE,
autoWidth = TRUE,
order = list(7, 'desc') #order by col 7
), escape = F
)
```
Add an interactive table
Add a heading before the table
Customise these options
Tip: clean headings first
Column names often
use underscores or
other characters which
are better replaced
with spaces when used
publicly. Use this code
before the table is
generated.
## Explore your area
```{r clean headings}
#clean column headings
colnames(subsetdf) <-
str_replace(colnames(subsetdf),"_"," ")
```
Tip: reduce file size
---
title: |
Regional analysis: `r params$reg`
output:
html_document:
self_contained: false
lib_dir: site/libs
params:
reg: "Anyville"
---
By default each HTML document
contains all the JavaScript it needs - but
this makes it huge. We stop it doing
that.
It now needs to know where to put any
files (JavaScript, CSS) that the HTML
document will need, so we specify a
folder called ‘libs’ inside a ‘site’ folder
Notebook 2: Index.Rmd
(in the /site folder)
Another R Markdown file
This is the site’s index.html page
● .Rmd file NEEDS TO BE IN SITE FOLDER
● Use markdown to create the content for your
page — no need for R code blocks
● Headings: # or ## or ###
● Italic, bold: * or ** or *** before and after
● Links: [link text](http://url)
● Indented text, e.g. quotes: >
● Bullet list items: *
Index.Rmd
Knit the file — it should generate a HTML
version called index.html
---
title: |
Stop and search - get the data
output:
html_document:
df_print: paged
---
The YAML
***This story is available for use by NEWSORG's news partners. Please do not
share outside of the network. It is under strict embargo until November 7 at
0001.***
***Use the dropdown navigation menus across the top of the page to see the
data for a specific local authority***
# What’s the story?
Here's some text for the first part of the page.
## What the experts say
Blah blah.
Example Markdown
Notebook 3: rendering
markdown files
This will render multiple files
● It will use the template Rmd file as the basis
● It needs a list of regions, and will generate one
for each
```{r import data}
#store the URL
fileloc =
"https://docs.google.com/spreadsheets/d/e/2PACX-1vR2-w4JWtqa
DmOBp7CxZpMaqsa-2Tm4vUVslOf5_NVfiTIavYEJWHWOv-4sGJ9VUUlx8eJP
nULECeYW/pub?gid=0&single=true&output=csv"
data = read_csv(fileloc)
#just get one column - it's now a vector
regions <- data$region
```
Import the data - again
This bit is new. We store a list
of regions to loop through
```{r generate md files}
#store the location of the template
paramsfile <- "01template.Rmd"
#loop through all regions
for (r in regions) {
rmarkdown::render(paramsfile, params = list(reg = r),
output_file =
paste(sep="",'site/',stringr::str_replace_all(r,"
","-"),'.md'),
envir = parent.frame())
}
```
Loop and render
Here is where we set the
params$reg to the region
currently being looped through
```{r generate md files}
#store the location of the template
paramsfile <- "01template.Rmd"
#loop through all regions
for (r in regions) {
rmarkdown::render(paramsfile, params = list(reg = r),
output_file =
paste(sep="",'site/',stringr::str_replace_all(r,"
","-"),'.md'),
envir = parent.frame())
}
```
(Troubleshooting)
This will cause an error if you
haven’t set the YAML to create the
/site folder
Solutions:
● Create /site folder, or:
● Edit YAML in template so it specifies a lib_dir
Notebook 4: rendering
HTML files and navigation
```{r get file names}
#get the names of all the html files
filenames <- list.files("site")
```
Make a list of files
The files were all rendered into
a folder called ‘site’ so we are
asking for a list of files in that
folder
```{r generate yaml}
#store the string we want to start out yaml file with
yamlstring <- 'name: "reg"
navbar:
title: "Stop and search"
left:
- text: "Regions"
menu:'
```
Generate the YAML…
This text will be used in the
navigation menu that’s created
The menu part is going to be
filled in next…
#create an empty vector to store all the strings we're about to create
strvec <- c()
#loop through the filenames
for (i in filenames){
if(substring(i,nchar(i)-2,nchar(i)) == ".md" ){
#replace spaces with dashes, and replace the file extension with .html
htmlversion <- gsub(" ","-",gsub(".md",".html",i))
#get the name by removing the file extension.
textversion <- gsub(".md","",i)
#create a string for the YAML file by inserting those
fullstring <- paste0('
- text: "',textversion,'"
href: ',htmlversion)
strvec <- c(strvec,fullstring) #add to the collection of strings
}
}
…gather links for it…
What’s happening?
● We create an empty list
● Loop through each filename and for each:
● Extract the name to create link text
● Generate a .html version of its filename to
create URL for it
● Add to the empty list
#add the initial string
strvec <- c(yamlstring, strvec)
#create a yaml file with that string of text in it
write(strvec, file = "site/_site.yml")
…and write as a .yml file
```{r render site}
#now render the site
rmarkdown::render_site("site")
```
Render the HTML pages
It will look in the /site folder for the
.yml file as the basis for rendering
Create a website using
GitHub Pages
Put website files in a
‘docs’ folder — and
upload to GitHub
● Cannot upload more than 100 files so upload in 3 batches:
● .html files first
● Then the /libs folder (the table uses these files)
● Then the /site_libs folder (navigation uses these)
More on GitHub Pages
pages.github.com
Debugging:
cleaning the HTML files
This HTML will need fixing
● <!DOCTYPE html> on page
● Menu doesn’t work on region pages
● Menu might be too long for index page
● (Other things you could do but not worth the time
include: change local CSS/JS links to remote
ones; remove duplicate links; etc)
On the repo
https://github.com/paulbradshaw/parameterisation/blob/main/rfiles/05cleaning.Rmd
On the repo
https://github.com/BBC-Data-Unit/police_misconduct/tree/main/rfiles
https://github.com/BBC-Data-Unit/child-speech/tree/main/parameterisation
All that in a nutshell
● Parameterisation allows you to generate
multiple files from a single template
● This can be used to build a website with a page
for every region/category/row in your data
● Use GitHub Pages to easily publish that - but
allow time for tweaking and problem-solving
More:
● R Markdown: The Definitive Guide has a chapter
on parameterised reports

More Related Content

Similar to How to generate a 100+ page website using parameterisation in R

fINAL Lesson_5_Data_Manipulation_using_R_v1.pptx
fINAL Lesson_5_Data_Manipulation_using_R_v1.pptxfINAL Lesson_5_Data_Manipulation_using_R_v1.pptx
fINAL Lesson_5_Data_Manipulation_using_R_v1.pptxdataKarthik
 
ACADILD:: HADOOP LESSON
ACADILD:: HADOOP LESSON ACADILD:: HADOOP LESSON
ACADILD:: HADOOP LESSON Padma shree. T
 
Class 12 computer sample paper with answers
Class 12 computer sample paper with answersClass 12 computer sample paper with answers
Class 12 computer sample paper with answersdebarghyamukherjee60
 
Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)
Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)
Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)Serban Tanasa
 
PPT on Data Science Using Python
PPT on Data Science Using PythonPPT on Data Science Using Python
PPT on Data Science Using PythonNishantKumar1179
 
Bridging Structured and Unstructred Data with Apache Hadoop and Vertica
Bridging Structured and Unstructred Data with Apache Hadoop and VerticaBridging Structured and Unstructred Data with Apache Hadoop and Vertica
Bridging Structured and Unstructred Data with Apache Hadoop and VerticaSteve Watt
 
Elasticsearch intro output
Elasticsearch intro outputElasticsearch intro output
Elasticsearch intro outputTom Chen
 
r,rstats,r language,r packages
r,rstats,r language,r packagesr,rstats,r language,r packages
r,rstats,r language,r packagesAjay Ohri
 
R programming & Machine Learning
R programming & Machine LearningR programming & Machine Learning
R programming & Machine LearningAmanBhalla14
 
Get started with R lang
Get started with R langGet started with R lang
Get started with R langsenthil0809
 
R package development, create package documentation isabella gollini
R package development, create package documentation   isabella golliniR package development, create package documentation   isabella gollini
R package development, create package documentation isabella golliniDataFest Tbilisi
 
Has Many And Belongs To Many
Has Many And Belongs To ManyHas Many And Belongs To Many
Has Many And Belongs To Manyguest80d303
 
Rpg Pointers And User Space
Rpg Pointers And User SpaceRpg Pointers And User Space
Rpg Pointers And User Spaceramanjosan
 
bbyopenApp_Code.DS_StorebbyopenApp_CodeVBCodeGoogleMaps.docx
bbyopenApp_Code.DS_StorebbyopenApp_CodeVBCodeGoogleMaps.docxbbyopenApp_Code.DS_StorebbyopenApp_CodeVBCodeGoogleMaps.docx
bbyopenApp_Code.DS_StorebbyopenApp_CodeVBCodeGoogleMaps.docxikirkton
 
Enterprise Postgres
Enterprise PostgresEnterprise Postgres
Enterprise PostgresOracle Korea
 
DATA-Analysis-All-Slidescoursera.pdf
DATA-Analysis-All-Slidescoursera.pdfDATA-Analysis-All-Slidescoursera.pdf
DATA-Analysis-All-Slidescoursera.pdfNamanKabadi1
 

Similar to How to generate a 100+ page website using parameterisation in R (20)

fINAL Lesson_5_Data_Manipulation_using_R_v1.pptx
fINAL Lesson_5_Data_Manipulation_using_R_v1.pptxfINAL Lesson_5_Data_Manipulation_using_R_v1.pptx
fINAL Lesson_5_Data_Manipulation_using_R_v1.pptx
 
ACADILD:: HADOOP LESSON
ACADILD:: HADOOP LESSON ACADILD:: HADOOP LESSON
ACADILD:: HADOOP LESSON
 
MLflow with R
MLflow with RMLflow with R
MLflow with R
 
Class 12 computer sample paper with answers
Class 12 computer sample paper with answersClass 12 computer sample paper with answers
Class 12 computer sample paper with answers
 
Lecture 9.pptx
Lecture 9.pptxLecture 9.pptx
Lecture 9.pptx
 
Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)
Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)
Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)
 
PPT on Data Science Using Python
PPT on Data Science Using PythonPPT on Data Science Using Python
PPT on Data Science Using Python
 
Bridging Structured and Unstructred Data with Apache Hadoop and Vertica
Bridging Structured and Unstructred Data with Apache Hadoop and VerticaBridging Structured and Unstructred Data with Apache Hadoop and Vertica
Bridging Structured and Unstructred Data with Apache Hadoop and Vertica
 
Elasticsearch intro output
Elasticsearch intro outputElasticsearch intro output
Elasticsearch intro output
 
r,rstats,r language,r packages
r,rstats,r language,r packagesr,rstats,r language,r packages
r,rstats,r language,r packages
 
Hive(ppt)
Hive(ppt)Hive(ppt)
Hive(ppt)
 
Hive(ppt)
Hive(ppt)Hive(ppt)
Hive(ppt)
 
R programming & Machine Learning
R programming & Machine LearningR programming & Machine Learning
R programming & Machine Learning
 
Get started with R lang
Get started with R langGet started with R lang
Get started with R lang
 
R package development, create package documentation isabella gollini
R package development, create package documentation   isabella golliniR package development, create package documentation   isabella gollini
R package development, create package documentation isabella gollini
 
Has Many And Belongs To Many
Has Many And Belongs To ManyHas Many And Belongs To Many
Has Many And Belongs To Many
 
Rpg Pointers And User Space
Rpg Pointers And User SpaceRpg Pointers And User Space
Rpg Pointers And User Space
 
bbyopenApp_Code.DS_StorebbyopenApp_CodeVBCodeGoogleMaps.docx
bbyopenApp_Code.DS_StorebbyopenApp_CodeVBCodeGoogleMaps.docxbbyopenApp_Code.DS_StorebbyopenApp_CodeVBCodeGoogleMaps.docx
bbyopenApp_Code.DS_StorebbyopenApp_CodeVBCodeGoogleMaps.docx
 
Enterprise Postgres
Enterprise PostgresEnterprise Postgres
Enterprise Postgres
 
DATA-Analysis-All-Slidescoursera.pdf
DATA-Analysis-All-Slidescoursera.pdfDATA-Analysis-All-Slidescoursera.pdf
DATA-Analysis-All-Slidescoursera.pdf
 

More from Paul Bradshaw

How to work with a bullshitting robot
How to work with a bullshitting robotHow to work with a bullshitting robot
How to work with a bullshitting robotPaul Bradshaw
 
ChatGPT (and generative AI) in journalism
ChatGPT (and generative AI) in journalismChatGPT (and generative AI) in journalism
ChatGPT (and generative AI) in journalismPaul Bradshaw
 
Data journalism: history and roles
Data journalism: history and rolesData journalism: history and roles
Data journalism: history and rolesPaul Bradshaw
 
Working on data stories: different approaches
Working on data stories: different approachesWorking on data stories: different approaches
Working on data stories: different approachesPaul Bradshaw
 
Visual journalism: gifs, emoji, memes and other techniques
Visual journalism: gifs, emoji, memes and other techniquesVisual journalism: gifs, emoji, memes and other techniques
Visual journalism: gifs, emoji, memes and other techniquesPaul Bradshaw
 
Using narrative structures in shortform and longform journalism
Using narrative structures in shortform and longform journalismUsing narrative structures in shortform and longform journalism
Using narrative structures in shortform and longform journalismPaul Bradshaw
 
Narrative and multiplatform journalism (part 1)
Narrative and multiplatform journalism (part 1)Narrative and multiplatform journalism (part 1)
Narrative and multiplatform journalism (part 1)Paul Bradshaw
 
Teaching data journalism (Abraji 2021)
Teaching data journalism (Abraji 2021)Teaching data journalism (Abraji 2021)
Teaching data journalism (Abraji 2021)Paul Bradshaw
 
Data journalism on the air: 3 tips
Data journalism on the air: 3 tipsData journalism on the air: 3 tips
Data journalism on the air: 3 tipsPaul Bradshaw
 
7 angles for data stories
7 angles for data stories7 angles for data stories
7 angles for data storiesPaul Bradshaw
 
Uncertain times, stories of uncertainty
Uncertain times, stories of uncertaintyUncertain times, stories of uncertainty
Uncertain times, stories of uncertaintyPaul Bradshaw
 
Ergodic education (online teaching and interactivity)
Ergodic education (online teaching and interactivity)Ergodic education (online teaching and interactivity)
Ergodic education (online teaching and interactivity)Paul Bradshaw
 
Storytelling in the database era: uncertainty and science reporting
Storytelling in the database era: uncertainty and science reportingStorytelling in the database era: uncertainty and science reporting
Storytelling in the database era: uncertainty and science reportingPaul Bradshaw
 
Cognitive bias: a quick guide for journalists
Cognitive bias: a quick guide for journalistsCognitive bias: a quick guide for journalists
Cognitive bias: a quick guide for journalistsPaul Bradshaw
 
The 3 chords of data journalism
The 3 chords of data journalismThe 3 chords of data journalism
The 3 chords of data journalismPaul Bradshaw
 
Data journalism: what it is, how to use data for stories
Data journalism: what it is, how to use data for storiesData journalism: what it is, how to use data for stories
Data journalism: what it is, how to use data for storiesPaul Bradshaw
 
Teaching AI in data journalism
Teaching AI in data journalismTeaching AI in data journalism
Teaching AI in data journalismPaul Bradshaw
 
10 ways AI can be used for investigations
10 ways AI can be used for investigations10 ways AI can be used for investigations
10 ways AI can be used for investigationsPaul Bradshaw
 
Open Data Utopia? (SciCAR 19)
Open Data Utopia? (SciCAR 19)Open Data Utopia? (SciCAR 19)
Open Data Utopia? (SciCAR 19)Paul Bradshaw
 
Scraping for journalists - ideas, concepts and tips (CIJ Summer School 2019)
Scraping for journalists - ideas, concepts and tips (CIJ Summer School 2019)Scraping for journalists - ideas, concepts and tips (CIJ Summer School 2019)
Scraping for journalists - ideas, concepts and tips (CIJ Summer School 2019)Paul Bradshaw
 

More from Paul Bradshaw (20)

How to work with a bullshitting robot
How to work with a bullshitting robotHow to work with a bullshitting robot
How to work with a bullshitting robot
 
ChatGPT (and generative AI) in journalism
ChatGPT (and generative AI) in journalismChatGPT (and generative AI) in journalism
ChatGPT (and generative AI) in journalism
 
Data journalism: history and roles
Data journalism: history and rolesData journalism: history and roles
Data journalism: history and roles
 
Working on data stories: different approaches
Working on data stories: different approachesWorking on data stories: different approaches
Working on data stories: different approaches
 
Visual journalism: gifs, emoji, memes and other techniques
Visual journalism: gifs, emoji, memes and other techniquesVisual journalism: gifs, emoji, memes and other techniques
Visual journalism: gifs, emoji, memes and other techniques
 
Using narrative structures in shortform and longform journalism
Using narrative structures in shortform and longform journalismUsing narrative structures in shortform and longform journalism
Using narrative structures in shortform and longform journalism
 
Narrative and multiplatform journalism (part 1)
Narrative and multiplatform journalism (part 1)Narrative and multiplatform journalism (part 1)
Narrative and multiplatform journalism (part 1)
 
Teaching data journalism (Abraji 2021)
Teaching data journalism (Abraji 2021)Teaching data journalism (Abraji 2021)
Teaching data journalism (Abraji 2021)
 
Data journalism on the air: 3 tips
Data journalism on the air: 3 tipsData journalism on the air: 3 tips
Data journalism on the air: 3 tips
 
7 angles for data stories
7 angles for data stories7 angles for data stories
7 angles for data stories
 
Uncertain times, stories of uncertainty
Uncertain times, stories of uncertaintyUncertain times, stories of uncertainty
Uncertain times, stories of uncertainty
 
Ergodic education (online teaching and interactivity)
Ergodic education (online teaching and interactivity)Ergodic education (online teaching and interactivity)
Ergodic education (online teaching and interactivity)
 
Storytelling in the database era: uncertainty and science reporting
Storytelling in the database era: uncertainty and science reportingStorytelling in the database era: uncertainty and science reporting
Storytelling in the database era: uncertainty and science reporting
 
Cognitive bias: a quick guide for journalists
Cognitive bias: a quick guide for journalistsCognitive bias: a quick guide for journalists
Cognitive bias: a quick guide for journalists
 
The 3 chords of data journalism
The 3 chords of data journalismThe 3 chords of data journalism
The 3 chords of data journalism
 
Data journalism: what it is, how to use data for stories
Data journalism: what it is, how to use data for storiesData journalism: what it is, how to use data for stories
Data journalism: what it is, how to use data for stories
 
Teaching AI in data journalism
Teaching AI in data journalismTeaching AI in data journalism
Teaching AI in data journalism
 
10 ways AI can be used for investigations
10 ways AI can be used for investigations10 ways AI can be used for investigations
10 ways AI can be used for investigations
 
Open Data Utopia? (SciCAR 19)
Open Data Utopia? (SciCAR 19)Open Data Utopia? (SciCAR 19)
Open Data Utopia? (SciCAR 19)
 
Scraping for journalists - ideas, concepts and tips (CIJ Summer School 2019)
Scraping for journalists - ideas, concepts and tips (CIJ Summer School 2019)Scraping for journalists - ideas, concepts and tips (CIJ Summer School 2019)
Scraping for journalists - ideas, concepts and tips (CIJ Summer School 2019)
 

Recently uploaded

How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 

Recently uploaded (20)

How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 

How to generate a 100+ page website using parameterisation in R

  • 1. Personalisation One dataset > 150 webpages with one click
  • 3.
  • 5. What we’ll cover Why we generated 150-page websites for partners How to do it: parameterisation + GitHub Pages Tips and tricks
  • 10. The steps: ● Produce a ‘template’ analysis for a given area ● Produce an index (home) page ● Render multiple .md versions of that analysis - one for each region ● Render HTML versions ● Publish as a website online (GitHub Pages) ● Debug, clean and tweak the code
  • 11.
  • 12. Go to this URL github.com/paulbradshaw/ parameterisation and: https://drive.google.com/drive/folders/17WtozAfB1frGZmLOJbSbJooP1WRDIfY1?usp=sharing
  • 13. The principles: ● Create a parameter — this should be what you want to generate a different page for (e.g. region, police force, category, politician) ● Limit code output to what should be in the HTML file (visible elements) ● Loop through list and call a ‘knitting’ function to create a file for each
  • 14. R Markdown files: a quick introduction
  • 15. Create an R Markdown file
  • 16. Give a title - and ✅ HTML
  • 17. Knit - and title it again Knit the file — you’ll be asked to name it again
  • 18. Markdown will generate HTML: ● Headings: # or ## or ### ● Italic, bold: * or ** or *** before and after ● Links: [link text](http://url) ● Indented text: > ● Bullet list items: * ● Code blocks: ```{r} above and ``` below ● Inline code: ` before and after
  • 19. Notebook 1: the template
  • 20. The YAML header --- title: "01template" author: "BIGNEWS" date: "2023-05-25" output: html_document ---
  • 21. ✅ Add a parameter --- title: "01template" author: "BIGNEWS" date: "2023-05-25" output: html_document params: reg: "Anyville" ---
  • 22. ✅ Change the title --- title: | Regional analysis: `r params$reg` author: "BIGNEWS" date: "2023-05-25" output: html_document params: reg: "Anyville" ---
  • 23. --- title: | Regional analysis: `r params$reg` author: "BIGNEWS" date: "2023-05-25" output: html_document params: reg: "Anyville" --- What have we added? Within that list an element called ‘reg’ with a value of ‘Anyville’ is created. We set it to that value for the purpose of testing. We define a list variable called ‘params’ The title fetches the ‘params’ list and then drills down into the ‘reg’ element within that, and fetches its value
  • 24. Knit to see how it’s changed!
  • 25. --- title: | Regional analysis: `r params$reg` output: html_document params: reg: "Anyville" --- Delete unwanted info
  • 26. ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) ``` # Analysing stop and search Police have been given new powers to stop and search potential criminals - but are the existing powers effective - and used fairly? Find out how the numbers changed in Add your own Markdown Level 1 header (1 hash, then space) Code block with knit settings (leave for now)
  • 27. # Analysing stop and search Police have been given new powers to stop and search potential criminals - but are the existing powers effective - and used fairly? Find out how the numbers changed in `r params$reg`. Use the parameter Inline R code: ‘calls’ the variable params$reg
  • 28. Code blocks: ```{r} Code goes here #add comments to explain ``` ● Code blocks and any messages/warnings will be visible in the HTML version — unless you specify otherwise in the Knitr settings
  • 29. Code: import packages ```{r import packages} #import the tidyverse library(tidyverse) #The datatables package library(DT) ``` Another code block - we’ve named it ‘import packages’ Code to import two packages - with comments
  • 31. Another code block ```{r import data} #store the URL fileloc = "https://docs.google.com/spreadsheets/d/e/2PACX- 1vR2-w4JWtqaDmOBp7CxZpMaqsa-2Tm4vUVslOf5_NVfiTIa vYEJWHWOv-4sGJ9VUUlx8eJPnULECeYW/pub?gid=0&singl e=true&output=csv" #Tidyverse's read_csv function imports from it data = read_csv(fileloc) ``` Import the data Store the URL of the CSV Read the CSV into a dataframe called ‘data’
  • 33. Add two parameters to suppress messages & warnings ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = FALSE, warning = F, message = F) ``` # Analysing stop and search Police have been given new powers to stop and search potential criminals - but are the Change Knitr settings Change echo= to FALSE (or F) to hide code from output
  • 34. ```{r filter and subset} #filter to those for this la, and specified cols subsetdf <- data %>% dplyr::filter(region == params$reg) ``` Parameter must match value in data to act as filter Use parameter to filter
  • 35. Extract figures to use ```{r extract figures} #store the figures for the earliest and latest years fig21 <- subsetdf$stops_2021 fig22 <- subsetdf$stops_2022 #calculate the change in this region localchange <- (fig22-fig21)/fig21 #calculate the change across all regions nationalchange <- (sum(data$stops_2022)-sum(data$stops_2021))/sum(data$stops_ 2021) ```
  • 36. ```{r create custom string} #set a variable to 'rose' or 'dropped' depending on relation of earlier figure to newer one changetype <- ifelse(isTRUE(fig22 > fig21), "rose", "dropped") #create a custom string with that customstring1 <- paste0("In **",params$reg,"** the number of stops ",changetype," from **",fig21,"** in 2021 to **",fig22,"** in 2022.") ``` `r customstring1` Code block: creates a text string, inserting variables for particular region Template text + variables Inline code: ‘calls’ that string variable
  • 37. ```{r create another string} nationalcomparison <- ifelse(localchange>nationalchange, "higher than", "below the") #create a custom string with that customstring2 <- paste0("The number of people being stopped in 2022 **",changetype," by ",round(localchange*100,1),"%** compared to the previous year. This is ",nationalcomparison," the national rise of **",round(nationalchange*100, 1),"%**.") ``` `r customstring2` Code block: creates a text string, inserting variables for particular region Another line… Inline code: ‘calls’ that string variable
  • 38. That custom string broken down paste0( "The number of people being stopped in 2022 **", changetype, " by ", round(localchange*100,1), "%** compared to the previous year. This is ", nationalcomparison, " the national rise of **", round(nationalchange*100, 1), "%**.")
  • 39. ## Explore your area ```{r table} #Create the datatable. Add a caption if you want DT::datatable(data, style = 'bootstrap', caption = 'Numbers of people being stopped and searched', filter = 'top', options = list(pageLength = 10, scrollX=TRUE, autoWidth = TRUE, order = list(7, 'desc') #order by col 7 ), escape = F ) ``` Add an interactive table Add a heading before the table Customise these options
  • 40. Tip: clean headings first Column names often use underscores or other characters which are better replaced with spaces when used publicly. Use this code before the table is generated. ## Explore your area ```{r clean headings} #clean column headings colnames(subsetdf) <- str_replace(colnames(subsetdf),"_"," ") ```
  • 41. Tip: reduce file size --- title: | Regional analysis: `r params$reg` output: html_document: self_contained: false lib_dir: site/libs params: reg: "Anyville" --- By default each HTML document contains all the JavaScript it needs - but this makes it huge. We stop it doing that. It now needs to know where to put any files (JavaScript, CSS) that the HTML document will need, so we specify a folder called ‘libs’ inside a ‘site’ folder
  • 42. Notebook 2: Index.Rmd (in the /site folder)
  • 44. This is the site’s index.html page ● .Rmd file NEEDS TO BE IN SITE FOLDER ● Use markdown to create the content for your page — no need for R code blocks ● Headings: # or ## or ### ● Italic, bold: * or ** or *** before and after ● Links: [link text](http://url) ● Indented text, e.g. quotes: > ● Bullet list items: *
  • 45. Index.Rmd Knit the file — it should generate a HTML version called index.html
  • 46. --- title: | Stop and search - get the data output: html_document: df_print: paged --- The YAML
  • 47. ***This story is available for use by NEWSORG's news partners. Please do not share outside of the network. It is under strict embargo until November 7 at 0001.*** ***Use the dropdown navigation menus across the top of the page to see the data for a specific local authority*** # What’s the story? Here's some text for the first part of the page. ## What the experts say Blah blah. Example Markdown
  • 49. This will render multiple files ● It will use the template Rmd file as the basis ● It needs a list of regions, and will generate one for each
  • 50. ```{r import data} #store the URL fileloc = "https://docs.google.com/spreadsheets/d/e/2PACX-1vR2-w4JWtqa DmOBp7CxZpMaqsa-2Tm4vUVslOf5_NVfiTIavYEJWHWOv-4sGJ9VUUlx8eJP nULECeYW/pub?gid=0&single=true&output=csv" data = read_csv(fileloc) #just get one column - it's now a vector regions <- data$region ``` Import the data - again This bit is new. We store a list of regions to loop through
  • 51. ```{r generate md files} #store the location of the template paramsfile <- "01template.Rmd" #loop through all regions for (r in regions) { rmarkdown::render(paramsfile, params = list(reg = r), output_file = paste(sep="",'site/',stringr::str_replace_all(r," ","-"),'.md'), envir = parent.frame()) } ``` Loop and render Here is where we set the params$reg to the region currently being looped through
  • 52. ```{r generate md files} #store the location of the template paramsfile <- "01template.Rmd" #loop through all regions for (r in regions) { rmarkdown::render(paramsfile, params = list(reg = r), output_file = paste(sep="",'site/',stringr::str_replace_all(r," ","-"),'.md'), envir = parent.frame()) } ``` (Troubleshooting) This will cause an error if you haven’t set the YAML to create the /site folder
  • 53. Solutions: ● Create /site folder, or: ● Edit YAML in template so it specifies a lib_dir
  • 54. Notebook 4: rendering HTML files and navigation
  • 55. ```{r get file names} #get the names of all the html files filenames <- list.files("site") ``` Make a list of files The files were all rendered into a folder called ‘site’ so we are asking for a list of files in that folder
  • 56. ```{r generate yaml} #store the string we want to start out yaml file with yamlstring <- 'name: "reg" navbar: title: "Stop and search" left: - text: "Regions" menu:' ``` Generate the YAML… This text will be used in the navigation menu that’s created The menu part is going to be filled in next…
  • 57. #create an empty vector to store all the strings we're about to create strvec <- c() #loop through the filenames for (i in filenames){ if(substring(i,nchar(i)-2,nchar(i)) == ".md" ){ #replace spaces with dashes, and replace the file extension with .html htmlversion <- gsub(" ","-",gsub(".md",".html",i)) #get the name by removing the file extension. textversion <- gsub(".md","",i) #create a string for the YAML file by inserting those fullstring <- paste0(' - text: "',textversion,'" href: ',htmlversion) strvec <- c(strvec,fullstring) #add to the collection of strings } } …gather links for it…
  • 58. What’s happening? ● We create an empty list ● Loop through each filename and for each: ● Extract the name to create link text ● Generate a .html version of its filename to create URL for it ● Add to the empty list
  • 59. #add the initial string strvec <- c(yamlstring, strvec) #create a yaml file with that string of text in it write(strvec, file = "site/_site.yml") …and write as a .yml file
  • 60. ```{r render site} #now render the site rmarkdown::render_site("site") ``` Render the HTML pages It will look in the /site folder for the .yml file as the basis for rendering
  • 61.
  • 62. Create a website using GitHub Pages
  • 63. Put website files in a ‘docs’ folder — and upload to GitHub ● Cannot upload more than 100 files so upload in 3 batches: ● .html files first ● Then the /libs folder (the table uses these files) ● Then the /site_libs folder (navigation uses these)
  • 64.
  • 65. More on GitHub Pages pages.github.com
  • 67. This HTML will need fixing ● <!DOCTYPE html> on page ● Menu doesn’t work on region pages ● Menu might be too long for index page ● (Other things you could do but not worth the time include: change local CSS/JS links to remote ones; remove duplicate links; etc)
  • 69.
  • 72. All that in a nutshell ● Parameterisation allows you to generate multiple files from a single template ● This can be used to build a website with a page for every region/category/row in your data ● Use GitHub Pages to easily publish that - but allow time for tweaking and problem-solving
  • 73. More: ● R Markdown: The Definitive Guide has a chapter on parameterised reports