SlideShare a Scribd company logo
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.pptx
dataKarthik
 
ACADILD:: HADOOP LESSON
ACADILD:: HADOOP LESSON ACADILD:: HADOOP LESSON
ACADILD:: HADOOP LESSON
Padma shree. T
 
MLflow with R
MLflow with RMLflow with R
MLflow with R
Databricks
 
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
debarghyamukherjee60
 
Lecture 9.pptx
Lecture 9.pptxLecture 9.pptx
Lecture 9.pptx
MathewJohnSinoCruz
 
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 Python
NishantKumar1179
 
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
Steve Watt
 
Elasticsearch intro output
Elasticsearch intro outputElasticsearch intro output
Elasticsearch intro output
Tom 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
 
Hive(ppt)
Hive(ppt)Hive(ppt)
Hive(ppt)
Abhinav Tyagi
 
Hive(ppt)
Hive(ppt)Hive(ppt)
Hive(ppt)
Abhinav Tyagi
 
R programming & Machine Learning
R programming & Machine LearningR programming & Machine Learning
R programming & Machine Learning
AmanBhalla14
 
Get started with R lang
Get started with R langGet started with R lang
Get started with R lang
senthil0809
 
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
DataFest 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 Space
ramanjosan
 
bbyopenApp_Code.DS_StorebbyopenApp_CodeVBCodeGoogleMaps.docx
bbyopenApp_Code.DS_StorebbyopenApp_CodeVBCodeGoogleMaps.docxbbyopenApp_Code.DS_StorebbyopenApp_CodeVBCodeGoogleMaps.docx
bbyopenApp_Code.DS_StorebbyopenApp_CodeVBCodeGoogleMaps.docx
ikirkton
 
Enterprise Postgres
Enterprise PostgresEnterprise Postgres
Enterprise Postgres
Oracle Korea
 
DATA-Analysis-All-Slidescoursera.pdf
DATA-Analysis-All-Slidescoursera.pdfDATA-Analysis-All-Slidescoursera.pdf
DATA-Analysis-All-Slidescoursera.pdf
NamanKabadi1
 

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

Telling factual stories in virtual reality, 360 degree video and augmented re...
Telling factual stories in virtual reality, 360 degree video and augmented re...Telling factual stories in virtual reality, 360 degree video and augmented re...
Telling factual stories in virtual reality, 360 degree video and augmented re...
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 robot
Paul Bradshaw
 
ChatGPT (and generative AI) in journalism
ChatGPT (and generative AI) in journalismChatGPT (and generative AI) in journalism
ChatGPT (and generative AI) in journalism
Paul Bradshaw
 
Data journalism: history and roles
Data journalism: history and rolesData journalism: history and roles
Data journalism: history and roles
Paul Bradshaw
 
Working on data stories: different approaches
Working on data stories: different approachesWorking on data stories: different approaches
Working on data stories: different approaches
Paul 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 techniques
Paul 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 journalism
Paul 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 tips
Paul Bradshaw
 
7 angles for data stories
7 angles for data stories7 angles for data stories
7 angles for data stories
Paul Bradshaw
 
Uncertain times, stories of uncertainty
Uncertain times, stories of uncertaintyUncertain times, stories of uncertainty
Uncertain times, stories of uncertainty
Paul 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 reporting
Paul 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 journalists
Paul Bradshaw
 
The 3 chords of data journalism
The 3 chords of data journalismThe 3 chords of data journalism
The 3 chords of data journalism
Paul 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 stories
Paul Bradshaw
 
Teaching AI in data journalism
Teaching AI in data journalismTeaching AI in data journalism
Teaching AI in data journalism
Paul 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 investigations
Paul Bradshaw
 
Open Data Utopia? (SciCAR 19)
Open Data Utopia? (SciCAR 19)Open Data Utopia? (SciCAR 19)
Open Data Utopia? (SciCAR 19)
Paul Bradshaw
 

More from Paul Bradshaw (20)

Telling factual stories in virtual reality, 360 degree video and augmented re...
Telling factual stories in virtual reality, 360 degree video and augmented re...Telling factual stories in virtual reality, 360 degree video and augmented re...
Telling factual stories in virtual reality, 360 degree video and augmented re...
 
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)
 

Recently uploaded

Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
The Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptxThe Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptx
DhatriParmar
 
JEE1_This_section_contains_FOUR_ questions
JEE1_This_section_contains_FOUR_ questionsJEE1_This_section_contains_FOUR_ questions
JEE1_This_section_contains_FOUR_ questions
ShivajiThube2
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
chanes7
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 

Recently uploaded (20)

Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
The Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptxThe Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptx
 
JEE1_This_section_contains_FOUR_ questions
JEE1_This_section_contains_FOUR_ questionsJEE1_This_section_contains_FOUR_ questions
JEE1_This_section_contains_FOUR_ questions
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 

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