pkgdownR/Medicine 2025
Senior Data Scientist, Statistics Canada
pkgdown site we will be working towards building togetherpkgdown sitepkgdown or GitHub Pages.R/ folder and the man/ folder).break
reference page)break
break
pkgdown sitethe end
Please ensure you have all of the following software installed (and updated to the most recent versions):
git_sitrep() function from the usethis package to verify your configuration.git_sitrep()git_sitrep()Vaccinated: TRUE. If not:git_sitrep()devtools package is installedFile > New project > New Directory > R package using devtoolsrmedicineDESCRIPTION fileRun
then update the Authors@R field
and change the version number to something a little less long.
README.md file#' Print hello world
#'
#' This function just prints hello world.
#'
#' @param x This argument does nothing.
#' If it did do something, it might require more words to explain.
#'
#' @return Prints `"Hello, world!"` to the console
#'
#' @examples
#' print_hello_world(x = 1)
#'
#' @export
print_hello_world <- function(x) {
print("Hello, world!")
}#' Print hello R/Medicine
#'
#' This function just prints hello R/Medicine.
#'
#' @param x This argument does nothing.
#' If it did do something, it might require more words to explain.
#'
#' @return Prints `"Hello, R/Medicine"` to the console
#'
#' @examples
#' print_hello_rmedicine(x = 1)
#'
#' @export
print_hello_rmedicine <- function(x) {
print("Hello, R/Medicine!")
}print_hello_rmedicine functioncheck() before pushing to GitHubWe should see no errors, warnings, or notes!
This will create the .git folder, and populate the .gitignore file.
The usethis package makes this step very simple for us!
pkgdown site using GitHub PagesAgain, the usethis package makes this very easy to do!
main branch, our website will be automatically updated for us!pull before you pushTo preview changes locally:

You can run the use_logo function, which will automatically place the file in the right location within your package.
Without loading the pkgdown package, run
In the _pkgdown.yaml file, add a bootswatch theme:
Tip
There are a couple of bootswatch themes that are so new, they haven’t been implemented in Quarto. Most of them should work, though!
bslib variablesbslibbslib is the easiest!extra.scss in the pkgdown folder└── 📁 rmedicine/
└── 📁 pkgdown/
└── 📄 extra.scss
Minimal content for extra.scss:
Let’s change the colour of the search bar!
Right click > Inspect.form-controllight-switch to true in our _pkgdown.yml file:In our extra.scss file:
We can set a syntax highlighting theme for light mode and dark mode.
pkgdown documentation for an example.In the _pkgdown.yml file:
titledesccontentsYou can use “topic matching” as a shortcut:
You can also use emojis, and create subsections!
search, github and lightswitchfooter:
structure:
left: developed_by
right: built_with_pkgdown
components:
developed_by: "Developed by [Melissa Van Bussel](https://melissavanbussel.com/) :computer: for the [R/Medicine 2025 Conference](https://rconsortium.github.io/RMedicine_website/)."
built_with_pkgdown: "This site was built using [pkgdown](https://pkgdown.r-lib.org/)! <img src='https://pkgdown.r-lib.org/logo.png' alt='pkgdown logo' width='25'>"extra.scss filepkgdown also automatically recognizes the pkgdown/extra.js file└── 📁 rmedicine/
└── 📁 pkgdown/
└── 📄 extra.scss
└── 📄 extra.js
#js_alert# – a pop-up message will appear when button is clickeddocument.addEventListener("DOMContentLoaded", function() {
let javascriptAlert = document.querySelectorAll(".nav-link[href*='#js_alert#']");
javascriptAlert.forEach(function (jsAlert) {
jsAlert.addEventListener("click", function(event) {
event.preventDefault();
alert("JavaScript is working!");
});
});
});Yes! You can insert additional HTML etc.
For example, we can add additional javascript using after_body in the _pkgdown.yml file:
template:
includes:
after_body: |
<script src="https://cdn.jsdelivr.net/npm/canvas-confetti@1.6.0/dist/confetti.browser.min.js"></script>
<script>
document.addEventListener("DOMContentLoaded", function () {
document.addEventListener("click", function (e) {
if (typeof confetti === "function") {
const x = e.clientX / window.innerWidth;
const y = e.clientY / window.innerHeight;
confetti({
origin: { x: x, y: y }
});
}
});
});
</script>(* = in our current, very specific scenario)
main, the terminal commands in the .github/workflows/pkgdown.yaml file are run on a virtual machinepkgdown::build_site_github_pages())/docs) 😊pkgdown and usethis packages take care of the hard work for us by writing this file for usIt’s pretty complicated:
.github/workflows/pkgdown.yaml file…The Quarto installation is just one small part of this action
Remember: GitHub Pages will host any page that ends up in our /docs folder.
The most important commands for R users are:
| Command | What it does |
|---|---|
mkdir folder_name |
Make a new folder called folder_name |
cd folder_name |
Change current directory to folder_name (use cd ../ to go up a folder level) |
cp old_folder/file_name new_folder/file_name |
Copy the file_name file from the old_folder to the new_folder |
Rscript -e |
Execute a line of R code |
quarto render proj_name --output-dir="docs" |
Render your Quarto file into the docs folder |
We’re going to change the link.svg image
docs/link.svgdocs folder is in our .gitignore, though, so just changing the file will only work locally.github/workflows/pkgdown.yaml filepkgdown/assets/custom_link.svg (any files in the pkgdown/assets folder will be automatically copied to the docs folder during the workflow).pkgdown::build_site_github_pages() function is called.Build > Install > Clean and installquarto package in R)DESCRIPTION file, you must add it as a dependency in .github/workflows/pkgdown.yaml.Tip
If there is an error in your Quarto file (i.e., it does not render), the error message will only say system command 'quarto' not found. Quarto is probably installed, and you probably have a syntax error in your Quarto vignette.
You can type Alt+0160 to add a blank character ( will not work).
navbar:
structure:
left: [vignettes, rd_files]
right: [search, github, my_linkedin, js_alert, lightswitch]
components:
vignettes:
text: " Vignettes"
icon: fa-book
menu:
- text: Examples
- text: " Tutorial Title"
href: articles/my_vignette.html
rd_files:
text: " Function Documentation"
icon: fa-file
menu:
- text: All functions
href: reference/index.html
- text: " Print things"
- text: " Print Hello World"
href: reference/print_hello_world.html
- text: " Print Hello R/Medicine"
href: reference/print_hello_rmedicine.htmlIn the pkgdown/extra.js file (replace with your package name, URL, and folder depth):
document.addEventListener("DOMContentLoaded", function () {
const brand = document.querySelector(".navbar-brand");
if (!brand) return;
const isFileProtocol = window.location.protocol === "file:";
const imgSrc = isFileProtocol ? "../".repeat(7) + "logo.png" : "logo.png";
brand.innerHTML = `
<img src="${imgSrc}" height="30" style="vertical-align: middle; margin-right: 10px;">
<span style="vertical-align: middle;">rmedicine</span>
`;
});You can add or remove any of the default components:
You can customize the information for each author, including by adding arbitrary HTML:
Examples where shields.io will do the work for you:
.json or .yaml file.json file or .yaml filedocs folder, shields.io will be able to access it!Let’s add a test coverage badge:
We need to:
covr to check our package coverage.json file in the docs folderIn the _pkgdown.yml file:
- name: Add test coverage badge
run: Rscript -e "jsonlite::write_json(list(coverage = sprintf('%.1f%%', covr::percent_coverage(covr::package_coverage()))), 'docs/coverage.json', auto_unbox = TRUE)"We now need to add the jsonlite, testthat, and covr packages to our workflow’s package installations.
In the .github/workflows/pkgdown.yaml file:
home:
sidebar:
structure: [badges, links, authors]
components:
badges:
title: Package badges
text: |
<br>
<br>
<br>.github.ioggnot2.site for a year for \(\approx\) $2 CAD from domain.comFrom your domain provider’s DNS settings (full details here):
A recordsA records (use @ rather than www)185.199.108.153
185.199.109.153
185.199.110.153
185.199.111.153
CNAME record (use www rather than @) and point it to your GitHub Pages URLmelissavanbussel.github.io/rmedicine
Tip
If your custom domain has a different depth than your GitHub Pages URL, you may need to adjust some file paths.
Settings > Pages > Custom domain and enter the domain without www or https:// in front of it (e.g., ggnot2.site)pkgdown siteslides/ folder of my package.pkgdown siteslides folder, then a slides.qmd file in this folder^slides$ to your .Rbuildignorequarto render command to your .github/workflows/pkgdown.yaml file, and output the files to the docs folderPlease use this form to provide anonymous feedback about today’s workshop.
I’ll likely be teaching this again in other contexts, so any feedback you have is greatly appreciated and will be used to improve future iterations 😊.
Any questions?