Why this class?
Goal: Learn some best practices to make your life in R easier and your research more reproducible
In the age of LLMs, it’s more important than ever to be able to reproduce your work and understand what you did
Note
We’re all figuring this out as we go along!
No one and no field is immune from errors in data analysis. Our goal is to make them as unlikely as possible (and report them when we find them!)
Note
I think we may have a different class of errors with AI, but I know (and have seen) that we will still have errors to deal with.
Your final project will be something like this, but a lot smaller and more fun 😉
(We will talk about what Git and GitHub are next, I promise!)
Install the {usethis} package:
install.packages("usethis")If you just updated R to a new “major” version, you will need to reinstall packages
RStudio tries to help!
Spelling the package or function’s name wrong, or not installing or loading the package
library vs. ::If you are writing a script you will save, and will use several functions from this package
I try to only run library(package) from a script (not the console) so that there’s a “record” of me loading the package, or else I might accidentally write code that doesn’t work later
:: still means you have to install itInstalling and loading are two different things:
install.packages("here") – once per computer (you download it)library(here) or here::here() – every session (you use it):: skips the library() step, not the install step.
Tip
This one trips people up, because there’s no library() line to tell you a package is involved. If you see “there is no package called …”, you need install.packages("...") first. You’ll hit exactly this with {tidycat} tomorrow.
Packages rely on other packages. When you install one, you get some of them but not all:
{gtsummary} has 10 Imports and 30 Suggests. In the exercises later today I’ll have you run:
Tip
dependencies = TRUE grabs the Suggests too. It takes longer, but it saves you hitting “there is no package called …” halfway through an example.
Introduce yourself:
usethis::use_git_config(user.name = "Louisa Smith", user.email = "louisahsmith@gmail.com")When you make changes to your code, they will be associated with this name and email address (this doesn’t really matter for our purposes)
Since I only need to run this once, I would probably run this from the console (bottom) rather than a script (top)
Running from the console is great for install.packages(), quick calculations, fiddling with code until you get it right, or scenarios like this – otherwise save your code in a script!
Create a github token:
usethis::create_github_token()Instead of entering your password every time, this is a secure way to connect to GitHub
Copy the token
Back in R, run this code and paste your token in the console where it says “Enter password”:
gitcreds::gitcreds_set()(You are not going to see the characters as you type; just hit enter after you paste)
You can do this again whenever your token expires or you are using a different device