Skip to content

Using Git projects within RStudio

In this tutorial we'll be showing you how to create a new Git project within RStudio using either a new or existing GitHub repository

GitHub configuration

1) Create a GitHub repository (if you do not already have one) on either the QMUL GitHub Enterprise instance or github.com. In this tutorial, we will be using a repository called rstudio_test.

2) Create a Personal Access Token (PAT) on GitHub by clicking on your profile icon in the top right, followed by Settings. In the left hand pane, click Developer settings and then Fine-grained tokens under the Personal access tokens dropdown.

or alternatively use this direct link to create a PAT on the QMUL GitHub Enterprise instance.

Enter a token name and select an expiration date (in this tutorial, we will be using a PAT called rstudio_pat). Optionally add a description for the PAT. Within the Repository access section, click the Only select repositories radio button and select your repository.

In the Permissions section, grant Read and write access to the Contents scope, so your PAT has the relevant access to your repository.

Click the Generate token button to create the PAT. On the next screen, you will see the generated token once, so ensure you take note of the token contents (starting github_pat_), as you won't have another opportunity to see this again.

RStudio configuration

1) Within RStudio, click File followed by New Project

2) In the New Project Wizard, select Version Control followed by Git

3) Enter your repository URL (HTTPS method) in the Repository URL box, select a project directory name (Rstudio suggests using the repository name as the project name, but you can change this if necessary) and select the project directory (we will use rstudio_test for the project name and ~/rstudio as the project directory). Click Create Project to continue.

4) Enter your GitHub username at the username prompt and your PAT at the password prompt

After the setup, you should see your repository contents within the Files tab in the bottom right of the RStudio application:

You may interface with GitHub using the buttons within the Git tab, in the top right of the RStudio application, or the Git button in the top menu bar.

The username and PAT needs to be entered every time you start a new RStudio session, but not on every Git operation. You may push/commit directly from RStudio, or make changes externally and pull within RStudio.

Performing Git operations within RStudio

After setting up your RStudio Git project and confirming connectivity with GitHub (see the above two sections for information on configuration), you are now ready to start using Git within Rstudio.

We will now demonstrate creating a new R script containing code date(), and committing this to GitHub from within RStudio.

1) Create a new R script by selecting File > New File > R Script, and enter the code date():

We can click the Run button to validate this works within the R shell. The output should display the current date and time. Below shows an example for this output, of when I ran the date() command:

> date()
[1] "Fri Mar  1 16:40:11 2024"

2) Save this file using File > Save As. Ensure you save this file inside your Git project directory (we are saving this file as date.R):

The file should now be shown in the Files browser in the bottom right:

Inside the Git tab, you should now see the date.R file showing as unstaged, with two yellow boxes in the Status field:

3) Click the Staged box and the Status field should turn green.

4) Click the Commit button to load a new window, showing the files staged, and their changes. Enter a useful commit message and click the Commit button. You will see the Git commit has been added in the pop-up box. Click Close and then close the commit window to return to the main Rstudio application.

Underneath the Commit button, you will now see that your branch is ahead of the main branch by one commit.

5) Click the Push button to push your changes to GitHub. Close the pop-up window that has confirmed the push was successful.

Within GitHub, we can now see the date.R file exists inside the repository:

If you have changed your files externally to RStudio, simply click he Pull button to pull in your changes inside your RStudio Git project.