Day 31 - Vignettes/Reprex

Spring 2023

Smith College

Overview

Timeline

  • What are Vignettes?
  • Authoring Vignettes

Goal

You will learn the whys and hows of creating vignettes for your package.

What are Vignettes?

A Roadmap for Your Package



A vignette acts as an introduction to your package. It is often the first thing people will read after the ReadME


It should introduce the problem your package is meant to solve, how your package can help with that problem, and showcase using your package from start to finish.


Where function help pages look only at specific functions, a vignette helps you show how those functions connect and interact.

Contents of a Vignette

Vignettes offer a lot of freedom in comparison to something like a help file.


They are typically written like an article, with an introduction, and multiple sections (notice the table on contents on the right!).


You can also include code chunks, code output, and visualizations.

Reproducible Examples (Reprex)

A cornerstone of good vignettes are self-contained reproducible examples (reprex).


You can share the step-by-step of running your code, but also the logic of why you made it that way.


If users understand how you think about your package, it can help them solve their own problems, or maybe even contribute to the package themselves!

graph TD
  A[Collect Data] --> B[Clean Data]
  B --> C[Analyze Data]
  C --> D[Plot Results]
  
    linkStyle 0 stroke:white
    linkStyle 1 stroke:white
    linkStyle 2 stroke:white

Making Multiple Vignettes

Most packages have a main introduction vignette that explains the broad purpose of the package.


However, it is also common to have multiple vignettes to explain different parts of the package in more depth.


Think about it like having maps at two different resolutions.

Authoring Vignettes

Home of ./vignettes/

Vignettes live in the ./vignettes/ folder as .rmd files.


You can reference any of your functions or package data in the vignettes.


Importantly, it is one fo the few places you can use functions like library(), as it is meant to mimic user interaction with your package.

R Project Name
.
├── R/
│   └── func1.R
├── man/
│   └── func1.Rd
├── inst/
│   ├── data/
│   │   └── example_data1.csv
│   └── other
├── vignettes/
│   └── v1.rmd
├── tests/
│   └── testthat/
│       └── test-func1.R
├── NAMESPACE
├── DESCRIPTION
├── LICENSE
├── NEWS.md
├── README.Rmd 
├── .gitignore
└── .Rbuildignore

Creating a Vignette

You can use usethis::use_vignette("NAME") to create the required directories and a template file.


Much like our the helpers, you will need to do the work of filling it out.


It is mostly a regular R markdown document, with some extra options in the YAML header.

Creates a .rmd file, not Quarto!

---
title: "THESE-MUST-MATCH"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{THESE-MUST-MATCH}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

'''{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
'''

'''{r setup}
library(mypackage)
'''

Creating a Vignette

You can use usethis::use_vignette("my-vignette") to create the required directories and a template file.


Much like our the helpers, you will need to do the work of filling it out.


It is mostly a regular R markdown document, with some extra options in the YAML header.

Creates a .rmd file, not Quarto!

---
title: "THESE-MUST-MATCH"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{THESE-MUST-MATCH}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

'''{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
'''

'''{r setup}
library(mypackage)
'''

Building Vignettes

You must render your vignette with the knit button.


However, be aware that the document will use the version of your package you currently have installed.


This means you will need to install your package again if you have made any major changes you want to see in the document.

Be Aware of Dependencies

You should only use packages that your package has an official dependency on.


Otherwise, you have no idea if the person installing your package will the the required packages installed.


Remember to add anything you need to your imports!

Closing Remarks

Keep Things in Perspective

  • Take a step back and look at your package from the perspective of an outsider.
  • Remember it is an education document, don’t skip over details.
  • Try showing it to someone not on your team, can they understand it?
  • Build as you go, don’t try to remember everything at the end.


For the Standards

You must contribute a significant portion of both code and text to at least one vignette.

For Next Time

Topic

Package Website

To-Do