Appendix B — Tools Used to Make This Book
This appendix provides a comprehensive guide to replicating the environment and code used in this book. To get started, you’ll need to install the R programming language along with RStudio IDE. Additionally, we’ll walk through the steps for setting up a Quarto book project, integrating it with GitHub for version control, and ensuring reproducibility with the {renv}
package. This setup will allow you to restore the project’s environment exactly as it was during the book’s creation, ensuring that all code examples run seamlessly.
B.1 RStudio Installation
First, you’ll need to download and install R and RStudio Desktop. You can do this by visiting the following link: RStudio Desktop Download.
B.2 How to Set Up This Project with Quarto
Quarto is the next-generation version of RMarkdown, designed for a wide range of publishing tasks, including creating notes, presentations, websites, and books. This book has been developed using Quarto, with the project versioned on GitHub. For more details on how to publish a Quarto book, refer to the official Quarto documentation.
To set up your project:
- In RStudio, create a new project in a new directory.
- Enable Git for version control.
- Select “Quarto Book Project” as the project type. This will automatically generate a
_quarto.yml
file with the following structure:
---
project:
type: book
---
- To preview your book, use the terminal to run
quarto preview
. This command will generate a_book
directory containing the compiled book files.
B.2.0.1 GitHub Useful Commands
You can manage your project using GitHub directly from RStudio or via command line. To connect your project with GitHub:
After creating your project, initiate a Git repository with:
git init git remote add origin https://github.com/yourusername/your-repo.git
Commit your files and push them to the GitHub repository:
git branch -M main git push -u origin main
B.2.0.2 Publish Your Book on GitHub Pages
To publish your Quarto book on GitHub Pages:
Modify the
_quarto.yml
file to specify the output directory asdocs
:--- project: type: book output-dir: docs ---
Add a
.nojekyll
file to prevent GitHub Pages from ignoring files:touch .nojekyll
Render your book with:
quarto render
This command will create a
docs
folder where the compiled book will be stored.
B.2.0.3 Adding a Package
If you want to add a custom R package to your book project, follow these steps:
Create a new package using
devtools
:::create("yourpkg") devtools
Add raw data processing scripts:
::use_data_raw() usethis
This command creates a
.R
script in thedata-raw
directory for processing your data.Save processed data for use in the package:
::use_data(yourdata) usethis
Document your package functions and datasets:
::use_r("yourdataset") usethis::document() devtools
Use vignettes for additional documentation:
vignette("rd-other") # For datasets vignette("rd")
Finally, build your package to include all new data and functions:
::load_all(".") devtools
B.2.0.4 Ensuring Reproducibility with renv
To ensure that all analyses and code examples in the book are reproducible, we’ve utilized the renv package. This package captures the specific versions of all R packages used in the project, storing them in an renv.lock file located in the root directory of the book’s GitHub repository.
To restore the project environment to its original state:
Clone the project repository.
Run the following command in your R console:
::restore() renv
This command reads the renv.lock file and reinstalls all packages with the exact versions used during the book’s development.
Using renv ensures that all code examples work as intended, regardless of future package updates, making it easier for readers to replicate analyses or adapt the code to their datasets.