Installing R, RStudio, git, and Connecting to GitHub

In this course, we will use the open-source software R and RStudio as well as version control with git and GitHub. Here you can find the installation guidelines.

What is R?

R is a free and open-source programming language designed to help you perform data-related tasks, such as wrangling, statistical analysis, visualization, and many more things (this website and pretty much all of the material for this class was created wit R). R is getting more popular and knowledge of R will help you in the job market. Think of it as a very powerful engine in a car.

What is RStudio?

We will communicate with R via RStudio, a so-called integrated development environment. This means, we will never be opening R directly, but rather only work in this program. RStudio is a very well-maintained commercial product, but the free version that we will be using, Rstudio Desktop, has more than enough features to make working with R more user-friendly. Think of it as a dashboard, which we will be using to communicate with the engine, R. Note that RStudio is not a requirement for programming with R, although it’s very commonly used.

What are git and GitHub?

Git is a version control system that makes it easy to track changes and work on code collaboratively. git is a software you run locally on your computer. Version control allows you to store versions of your project as you progress, so you can come back to any version of the project, and accompany everything with human readable messages.

With git, writing code for a project will look somewhat like this:

GitHub is a hosting service for git. You can think of it as a Dropbox for code (but much better). It contains projects, which we call repositories or repos, and these repos can be public or private. This is a tool I will use to distribute code and homework assignments to you and collect them. And you will use it to keep track of your code and collaborate in teams. Github has a nice integration with Rstudio, so using this tool will minimize the amount of work you will need to do.

We will have a course organization on Github (like a course page on ILIAS), so your workflow will look something like this. We will go over it in class.

1. Install R

  1. Open your browser and go to https://cran.rstudio.com.

  2. Click on Download R for Windows and select base. This is the version you want when you are installing R for the first time,

  3. Click on Download R-4.3.1 for Windows.

  4. Run the installation from the .exe file you just downloaded.

  5. Now that R is installed, proceed to installing RStudio.

  1. Open your browser and go to https://cran.rstudio.com.

  2. Check whether your Mac has an Intel-based processor or an M1 (or higher). You can find this info under > About This Mac > Overview > Processor.

  1. Click on Download R for macOS and scroll down to the respective .pkg file in the list of files and download it.

  2. Run the installation from the .pkg file you just downloaded.

  3. Download and install XQuartz: https://www.xquartz.org/.

  4. Now that R is installed, proceed to installing RStudio.

2. Install RStudio

  1. Go to https://posit.co/ and click on the Download RStudio button on the top bar.
  2. Scroll down and click on the Download button for the free RStudio Desktop version.
  3. Since we’ve already completed Step 1: Install R, move to Step 2 and download the latest version of Rstudio Desktop for your operating system.
  4. Run the installer. Click yes through all the prompts to install like any other program.

3. Install git

  1. Go to https://git-scm.com/downloads and click on your operating system.
  2. Download the latest release (at the time of writing, it is 2.42.0). Follow the instructions and install git.
    • When asked about “Adjusting your PATH environment”, make sure to select “Git from the command line and also from 3rd-party software”.

Recommended option: Install the Xcode command line tools (not all of Xcode), which includes Git. This requires you opening Terminal on your Mac, i.e. we will install stuff by typing what to do rather than clicking on the buttons in programs. Don’t get intimidated, it’s easier than you think.

  1. Go to the Terminal and type these commands to elicit an offer to install developer command line tools:
git --version
git config
  1. Accept the offer. Click on “Install”.

Alternative approach (no command line required).

  1. Go to https://git-scm.com/downloads and click on your operating system.
  2. Download the latest release (at the time of writing, it is 2.42.0). Follow the instructions and install git.
    • When asked about “Adjusting your PATH environment”, make sure to select “Git from the command line and also from 3rd-party software”.

4. Check that git was installed properly

To make sure that you did everything right and you’re all set, do this:

  1. Open RStudio. The icons look similar, you need the right one:

  2. Go to lower left part of the window and click on Terminal.

  3. Type git in the Terminal. You should see something like this: If you are typing in the Console pane instead of Terminal, you will see this error. Make sure to switch to Terminal.

5. Get a GitHub account

  1. Go to https://github.com/ and create an account.
  2. Use your ILIAS-login as a username. Better use your university email, too.

6. Connect git to your Github account

We have installed everything we need to know and you have created a GitHub account. However, we still have not connected your Git and GitHub just yet. To interact with GitHub via Rstudio, you need to authenticate yourself, which means you need to prove you are the owner of your GitHub account. When you log in to GitHub from your browser, you provide your username and password to prove your identity. Here is what you need to do to authenticate yourself inside Rstudio:

1. Authenticate yourself to git on your computer.

Make sure to use the email associated with your GitHub account. Type the following in the Console pane in Rstudio:

install.packages("usethis")

usethis::use_git_config(user.name = "GitHub username", user.email = "Email associated with your GitHub account")

For example, I would type:

install.packages("usethis")

usethis::use_git_config(user.name = "vktrsmnv", user.email = "semenova@uni-mannheim.de")

2. Generate a personal access token (PAT).

Now you will essentially create a long password which you won’t need to re-enter when working with R.

Type the following in the Console pane in Rstudio:

usethis::create_github_token()

This will open a new page on Github in your browser, where you’ll need to select the validity period of your PAT - Expiration (please keep the default 30 days for now), and click on Generate Token at the bottom of the page.

Next page will give you the PAT. It will be a long string and will look something like this: ghp_Kt33T3rXI1m4a9vxpBU0ngRU0. Don’t close this page yet! You’ll need this sequence, so copy it.

3. Save PAT in R.

Now that you have the PAT, you need to tell it to R. Here we will need to install another package, which will do all the work for us.

Type the following in the Console pane:

install.packages("credentials")

credentials::set_github_pat()

Respond to the prompt with your personal access token (PAT).

If successful, your initial (and subsequent) calls will look like this:

set_github_pat()

# If prompted for GitHub credentials, enter your PAT in the password field
# Using GITHUB_PAT from Viktoriia Semenova (credential helper: manager-core)

If for whatever reason this approach does not work, try another package gitcreds.

Type the following in the Console pane:

install.packages("gitcreds")

gitcreds::gitcreds_set()

Here you will need to enter your PAT directly in the Console. The output should look like this.

> gitcreds::gitcreds_set()

? Enter password or token: ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-> Adding new credentials...
-> Removing credentials from cache...
-> Done.

Cloud Version of RStudio

R is free, but it can sometimes be a pain to install and configure. To make life easier, you can use the free RStudio.cloud service, which lets you run a full instance of RStudio in your web browser. This means you won’t have to install anything on your computer to get started with R! The free version of the Cloud, however, comes with limitations of project working time, so I do not recommend it as a default choice. It will, however, suffice if you want to follow along in class but want to work from your tablet rather than a laptop or may be helpful if something is not compiling on the local machine.

To use the cloud version, here is what you need to do:

  1. Make sure you have a GitHub account.

  2. Open Posit Cloud

2.1 The easiest way to do it is to go to the website of the course and find the cloud icon at the header of the page:

2.2 Or go directly to http://posit.cloud/

  1. Press Get started, at the next page press Sign Up button. There is not need to change anything here.

  1. Choose Sign Up with GitHub as a Sign Up option

  1. For the first time, you have to Sign in to Github with your Username(or email) and Password

  1. Github might ask you for a verification. Check your email connected to Github to find authentication code.

Congratulations! You now have your Posit Cloud! Still, you have some steps to do to make use of it.

  1. In Your Workspace, click your name on the upper right corner and select Authentication

  2. Here the check box Enabled next to the Github must be already ticked.We want the same to happen with the check box Private repo access also enabled.

  1. Here you may find if organizational access is provided to you. If not, you can request it. After that, click Authorize rstudio. Now both check boxes next to Github from the previous step must be ticked.

  2. Now you can use Posit Cloud. In Your Workspace, find New Project button on the upper right corner. You can both create New R Project and New Project from Git Repository. For the last option, click “New Project from Git Repository” and copy paste the link for needed repository from Github.

Welcome! This is a basic RStudio view but in your Posit Cloud!