Setting Up R, RStudio, Git, and GitHub

1 Install or update R

To install R, visit cloud.r-project.org to download the most recent version for your operating system. If you already have R, check that you have at least version 4.0.0 by running the following R code below. If your R version is 3.X, please update to avoid any issues related to earlier version of the R.

# Run me in RStudio's "Console"
version$version.string

2 Install or update RStudio

While R is a programming language, RStudio is a software (often referred to as an IDE, Integrated Development Environment) that provides R programmers with a neat, easy-to-use interface for coding in R. There are a number of IDEs out there, but RStudio is arguably the best and definitely most popular among R programmers.

Note: RStudio will not work without R installed, and you won’t particularly enjoy using R without having RStudio installed. Be sure to install both!

Image Credit: Manny Gimond | Accessible at https://mgimond.github.io/ES218/R_vs_RStudio.html
  • New install: To install RStudio, visit https://posit.co/download/rstudio-desktop/. Download the free (“Open Source Edition”) Desktop version for your operating system. You should install the most up-to-date version available that is supported by your operating system.

  • Update: If you already have RStudio and need to update: Open RStudio, and under ‘Help’ in the top menu, choose ‘Check for updates.’ If you have the most recent release, it will return ‘No update available. You are running the most recent version of RStudio.’ Otherwise, you should follow the instructions to install an updated version.

  • Open RStudio: If upon opening RStudio you are prompted to install Command Line Tools, do it.

Notefor Mac user

Potential issue:
Mac users may be prompted to install additional tools when using Git.

Steps if needed:

  • To install command line tools (if you’re not automatically prompted), run in the Terminal tab in RStudio: xcode-select --install

  • Visit xquartz.org to download & install XQuartz

3 Check for git

Important
  • macOS and Linux usually come with Git pre-installed
  • Windows may or may not include Git, depending on the version

You should already have git on your device, but let’s check for it anyway.

  • Open RStudio

  • In the Terminal tab, run the following command:

which git
  • If after running that you get something that looks like a file path to git on your computer, then you have git installed. For example, that might return something like this (or it could differ a bit): /usr/local/bin/git. If you instead get no response at all, you should download & install git from here: git-scm.com/downloads
Important

Is it necessary to have Git installed in your machine for this workshop. GitHub’s Git Guides are a really wonderful resource to start learning about this topic.

WarningTroubleshooting

If you download Git and the Git commands still not recognized by your computer, check your computer’s PATHS.

To do this, follow the instructions in this link on how to set the right PATHS.

Restart your computer and try running git --version on the terminal. You should get something like git version XX.XX (but with numbers instead of Xs).

If you see the git version printed out in your terminal, you are all set

4 Create a GitHub account

If you don’t already have a GitHub account, go to github.com and create one. Here are helpful considerations for choosing a username: happygitwithr.com/github-acct.html.

5 Connect Git and GitHub in RStudio

The last step to take before you’re all set for the workshop is to get these components talking to one another! You will have to introduce you GitHub credentials to your local computer, using RStudio and then create a Personal Access Token (PAT) on your GitHub account that will allow to transfer the changes you have made to your code in your computer to GitHub. The steps below will guide you on how to get this set up.

5.1 Install R packages

  • Install the usethis and gitcreds packages in R by running the following in the RStudio Console:
install.packages(“usethis”)

install.packages("gitcreds")

A lot of scary looking red text will show up while this is installing - don’t panic. If you get to the end and see something like below (with no error) it’s installed successfully.

5.2 Setup your git config and GitHub PAT

In the RStudio Console

Step 1: set the user’s global user.name and user.email and define integrate changes from one branch into another branch for all repositories.

1usethis::use_git_config(user.name = "my_user_name",
2                        user.email = "my_email@nceas.ucsb.edu",
3                        pull.rebase = "false")
1
Add you exact same GitHub user name. Case and spelling matters!
2
Set up your email address associated to you GitHub account.
3
Setting “merge” as the default strategy to integrate changes from one branch into another branch (for all repos). Check the note at the end of this chapter for more details.

Step 2: define the name of the branch that gets created when you make the first commit in a new Git repo

usethis::git_default_branch_configure(name = "main")

Step 3: check to make sure everything looks correct

usethis::git_sitrep()

Step 4: Setting up your PAT

Before you can push or pull between RStudio and GitHub, you’ll need to set up a Personal Access Token (PAT) for secure authentication.

  1. Run usethis::create_github_token() in the Console.
  2. A new browser window should open up to GitHub, showing all the scopes options. You can review the scopes, but you don’t need to worry about which ones to select this time. The previous function automatically pre-selects some recommended scopes. Go ahead and scroll to the bottom and click “Generate Token”.
  3. Copy the generated token.
  4. Back in RStudio, run gitcreds::gitcreds_set() in the Console. And wait until you are prompted to paste your token.
  5. Paste your PAT when the prompt asks for it.
  6. Last thing, run usethis::git_sitrep() in the Console to check your Git configuration and that you’ve successful stored your PAT. Note: look for Personal access token for 'https://github.com': '<discovered>'

Once you’ve completed these steps you are ready for our workshop on Git and Github