Lecturer in Health Data Science within Centre for Health Informatics, Computing, and Statistics.
Academic background in statistics, with experience in data science consultancy and training.
Using R for over 10 years, and author of multiple R packages.
Combines slides, live coding examples, and exercises for you to participate in.
Ask questions throughout!
Course website: nrennie.rbind.io/r-pharma-2024-parameterized-reports
A plot where aspects of the visualisation are controlled by one or more parameters.
Controls the underlying data, captions, title, colours, ….
Essentially so that you Don’t Repeat Yourself:
Once you’ve parameterized a plot, it’s quick to make different versions of the same plot.
Scripts are shorter and easier to read because you’re not copying and pasting code.
Ensure consistent layout between plots.
Create variables that define the data you’re plotting:
Variables don’t have to be data-related, they can also be:
Replacing hard-coded values with variables
Transforming code into a function
Using glue()
for parameterized plot text
Create a variable to define a year in the gapminder
data that you want to plot.
Edit the code in the gapminder_year.R
file to use this new year variable.
Turn your code into a function that takes the data and year variable as arguments (and check it works!)
Bonus: Add another argument to your function that allows a user to define the bar colour.
Bonus: How could you improve your function?
10:00
Quarto is an open-source scientific and technical publishing system that allows you to combine text, images, code, plots, and tables in a fully-reproducible document. Quarto has support for multiple languages including R, Python, Julia, and Observable. It also works for a range of output formats such as PDFs, HTML documents, websites, presentations,…
YAML header
Content
Text, links, images
Code, tables, plots
Equations, references
Documents: HTML, PDF, MS Word, Markdown
Presentations: Revealjs, PowerPoint, Beamer
Websites
Books
…
Note: you can do something similar with R Markdown.
Subset data based on parameters:
Using in code:
Adding a continent
parameter to the document
Accessing and using the parameter in plotting
Using the parameter for tables
Edit the report.qmd
file to add a parameter for year.
Add your plotting function to the quarto document, and pass the parameter into the plotting function. Check it renders.
Change the value of the parameter, and re-render.
05:00
Sometimes you want to use values from code in a sentence. We can add inline code:
The number of rows is
`r nrow(gapminder)`
.
which displays as:
The number of rows is 1704.
Display content only for a specific format:
::: {.content-visible when-format="html"}
Will only appear in HTML.
:::
::: {.content-hidden unless-format="pdf"}
Will be hidden unless it's a PDF document.
:::
Display content only for a specific parameter value:
`r if (params$hide_answers) "::: {.content-hidden}"`
The answer is 4.
`r if (params$hide_answers) ":::"`
Main project configuration: _quarto.yml
solutions
profile configuration: _quarto-solutions.yml
::: {.content-hidden when-profile="solutions"}
This content will be hidden in the advanced profile
:::
Writing inline R code
Using parameter values in inline R code
Using parameter values in figure captions
Edit the sentence ...a plot of the median life expectancy in the year 1952 for each continent...
to use inline code to add the document parameter for the year.
Use inline code for the title as well.
Edit the figure caption to use inline code to add the document parameter for the year.
10:00
Let’s say you want to create multiple different versions of your report.
Manually change parameter values and hit render?
Pass parameters in one at a time to the command line?
{quarto}
R packageThe {quarto}
R package provides an interface to the Quarto CLI.
We can use the quarto_render()
function as an alternative to the Render button or command line.
Then a for
loop?
{purrr}
packageReplace for
loops with code that is shorter and easier to read.
Map over a vector of different e.g. continents, time periods, …
Use the walk()
functions from {purrr}
to call a function for it’s side effect e.g. rendering a report.
Note: if you’re more a base R person, you can do something similar with the
apply()
family of functions.
Using the {quarto}
R package.
Using {purrr}
to generate multiple reports.
Render a version of your report for the year 1972
using the quarto_render()
function.
Render a version of your report for all years in the data.
10:00
Workshop website: nrennie.rbind.io/r-pharma-2024-parameterized-reports