7 Git, Github, and RStudio
Once you have Git installed on you computer and have create a Github account, the next step is to connect Git and your Github account details to RStudio. This will allow you to use version control directly within RStudio and push your repositories directly to Github.
7.1 Connecting Git, Github, and RStudio
The definitive source everything Git and Github related to R is a book called “Happy Git and GitHub for the useR” by Jenny Bryan. The e-book is freely available on online (it’s actually written in R and hosted on Github!), and is a very valuable resource. Look here for guidence and recommendations!
Dr. Bryan commits an entire section of the book to connecting R to Git and Github. Most of the follow text is taken directly from this section or lightly adapted. It is definitely worth your time to read through this section first
7.1.1 Detecting Git from RStudio
7.1.2 The usethis
package
The usethis
package provides many useful functions for using git and Github directly within your R project. Install this package:
install.packages("usethis)
7.1.2.1 Creating a personal access token (PAT)
The easiest way to connect Github and RStudio is by passing your Github credentials to RStudio using a personal access token. The usethis
package provides a function for this:
usethis::create_github_token()
This will launch a browser windows, which will ask you to log onto Github and select the scopes for your PAT. At a minimum, select “repo”, “user”, and “workflow”. Then click “Generate token”.
Copy the generated token, then pass it directly to R using:
> gitcreds::gitcreds_set()
? Enter password or token: ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-> Adding new credentials...
-> Removing credentials from cache...
-> Done.
Refer to this chapter for more details on the PAT and troubleshooting.
7.2 Connecting to Github
Followin the steps in the “Connect to Github” chapter to practice the general workflow for using R and Github.
This section covers creating a new repository, making local changes, and pushing them to Github.