Summary so far
We’ve covered a lot this morning, and most of it was setup rather than analysis. Here’s what you should have working before we move on to tables, plus what to check if something isn’t.
I drafted this recap with Claude, from the course content I’d already written, then rewrote and checked it.
Connecting R to GitHub
You ran these once, and shouldn’t need them again on this computer:
install.packages("usethis")
usethis::use_git_config(user.name = "Your Name", user.email = "you@email.com")
usethis::create_github_token()
gitcreds::gitcreds_set()The name and email just get attached to your commits. The token is the part that matters: it’s what RStudio sends instead of your GitHub password. If RStudio ever asks you for a password, it wants the token.
Along the way we talked about running code from the console versus a script. Anything you’d want to run again goes in a script. One-offs like install.packages() can go in the console.
Git and GitHub
The vocabulary, since it comes at you fast:
- fork – make your own copy of someone else’s repo, on GitHub
- clone – download a repo from GitHub to your computer
- stage – check the box next to a file to mark it for saving
- commit – save those changes, with a message describing them
- push – send your commits up to GitHub
You forked the class repo, cloned your fork, edited the README, and pushed the change. That loop – edit, stage, commit, push – is the whole workflow. Everything else is detail.
In the Git pane, M is a modified file, ? is a file Git hasn’t seen before, A is a new file you’ve staged, and D is a deleted one.
R Projects
You should now be opening RStudio by double-clicking an .Rproj file, not by opening RStudio and hunting for your files.
A project gives you a known working directory and keeps everything for one piece of work in one folder. You also changed the setting so R starts fresh each session, which means your code has to actually recreate everything it needs, rather than quietly depending on some object you made an hour ago and forgot about.
here::here()
The problem with setwd("/Users/louisa/...") is that the path is true on exactly one computer, and only until you move the folder.
# breaks the moment anyone else runs it, including future you
setwd("/Users/louisa/Documents/project")
data <- read.csv("data/raw/nlsy.csv")
# works from anywhere inside the project
data <- read.csv(here::here("data", "raw", "nlsy.csv"))here::here() builds paths from wherever your .Rproj file lives, so it resolves correctly on your machine and on mine.
Starting a project from scratch
We went local-first: make the R project with Git enabled, commit, then create an empty repo on GitHub and connect the two by pasting the three lines from GitHub into the terminal (not the console). Run them one at a time.
You also added secrets.txt to .gitignore. Anything with data you can’t share, passwords, or API keys belongs there. Files over 100 MB won’t push at all.
Your file structure should look something like:
epi590r-in-class/
├─ epi590r-in-class.Rproj
├─ README.md
├─ R/
│ └─ clean-data-bad.R
└─ data/
├─ raw/
│ └─ nlsy.csv
└─ clean/
If something isn’t working
- RStudio asks for a GitHub password. Give it the token, not your password. Lost it? Run
usethis::create_github_token()andgitcreds::gitcreds_set()again. here::here()points somewhere strange. You probably don’t have the project open. Check the top right of RStudio for the project name.- A file won’t show up in the Git pane. Check whether it’s listed in
.gitignore. - Push is greyed out or errors. Make sure you’ve committed first, and that your local repo is connected to a GitHub repo.
- Something is broken and you can’t tell why. Restart R (Session > Restart R) and run your code from the top. This fixes a genuinely surprising fraction of problems.
The short version
- Work in an R Project, always.
- Use
here::here()for file paths, neversetwd(). - Commit whenever you’d be annoyed to lose what you just did.
- Keep data and secrets out of GitHub.
- Start every session fresh, and let your code rebuild what it needs.