Skip to main content

Streamlining Your Workflow: Linking Git/GitHub with R Studio for Efficient Version Control

The Beginner’s Guide Linking Git/GitHub with R Studio:

Now that we have both R Studio and Git set-up on your computer and a GitHub account, it’s time to link them together so that you can maximize the benefits of using R Studio in your version control pipelines.

First we will link R studio and Git and then we will link R Studio and GitHub. We will also link an existing Project with Git and GitHub.



Linking R Studio and Git

In R Studio, go to Tools > Global Options > Git/SVN

Use the Global Options menu to tell R Studio you are using Git as your version control system

Sometimes the default path to the Git executable is not correct. Confirm that git.exe resides in the directory that R Studio has specified; if not, change the directory to the correct path. Otherwise, click OK or Apply.

Confirm that the directory R Studio points to for the Git executable is correct

R Studio and Git are now linked.

Linking R Studio and GitHub

In that same R Studio option window, click “Create RSA Key” and when this completes, click “Close.”

Following this, in that same window again, click “View public key” and copy the string of numbers and letters. Close this window.

Generate an RSA key and copy the public key to your clipboard

You have now created a key that is specific to you which we will provide to GitHub, so that it knows who you are when you commit a change from within R Studio.

To do so, go to github.com/, log-in if you are not already, and go to your account settings. There, go to “SSH and GPG keys” and click “New SSH key”. Paste in the public key you have copied from R Studio into the Key box and give it a Title related to R Studio. Confirm the addition of the key with your GitHub password.

Location of “SSH and GPG keys” on your profile settings
Telling GitHub the public SSH key generated in R Studio

GitHub and R Studio are now linked. From here, we can create a repository on GitHub and link to R Studio.

Create a new repository and edit it in R Studio

On GitHub, create a new repository (github.com > Your Profile > Repositories > New). Name your new test repository and give it a short description. Click Create repository. Copy the URL for your new repository.

Location of the “Repositories” link on your profile
Creating a new repository on GitHub

In R Studio, go to File > New Project. Select Version Control. Select Git as your version control software. Paste in the repository URL from before, select the location where you would like the project stored. When done, click on “Create Project”. Doing so will initialize a new project, linked to the GitHub repository, and open a new session of R Studio.

Creating a version controlled project on R Studio
Cloning your Git repository to R Studio

Create a new R script (File > New File > R Script) and copy and paste the following code:

print("This file was created within R Studio")

print("And now it lives on GitHub")

Save the file. Note that when you do so, the default location for the file is within the new Project directory you created earlier.

Saving your first script for this project

Once that is done, looking back at R Studio, in the Git tab of the environment quadrant, you should see your file you just created! Click the checkbox under “Staged” to stage your file.

All files that have been modified since your last pull appear in the Git tab

Click “Commit”. A new window should open, that lists all of the changed files from earlier, and below that shows the differences in the staged files from previous versions. In the upper quadrant, in the “Commit message” box, write yourself a commit message. Click Commit. Close the window.

Commiting your R Script to the repository!

So far, you have created a file, saved it, staged it, and committed it. If you remember your version control lecture, the next step is to push your changes to your online repository. Push your changes to the GitHub repository.

How to push your commit to the GitHub repository

Go to your GitHub repository and see that the commit has been recorded.

You’ve just successfully pushed your first commit from within R Studio to GitHub!

Projects under version control

Till now, we linked R Studio with Git and GitHub. In doing this, we created a repository on GitHub and linked it to R Studio. Sometimes, however, you may already have an R Project that isn’t yet under version control or linked with GitHub. Let’s fix that!

Linking an existing Project with Git

So what if you already have an R Project that you’ve been working on, but don’t have it linked up to any version control software (tut tut!)?

Thankfully, R Studio and GitHub recognize this can happen and have steps in place to help you (admittedly, this is slightly more troublesome to do than just creating a repository on GitHub and linking it with R Studio before starting the project…).

So first, let’s set up a situation where we have a local project that isn’t under version control. Go to File > New Project > New Directory > New Project and name your project. Since we are trying to emulate a time where you have a project not currently under version control, do NOT click “Create a git repository”. Click Create Project.

Creating a project that is not under version control

We’ve now created an R Project that is not currently under version control. Let’s fix that. First, let’s set it up to interact with Git. Open Git Bash or Terminal and navigate to the directory containing your project files. Move around directories by typing cd ~/dir/name/of/path/to/file

When the command prompt in the line before the dollar sign says the correct directory location of your project, you are in the correct location. Once here, type git init followed by git add . - this initializes (init) this directory as a git repository and adds all of the files in the directory (.) to your local repository. Commit these changes to the git repository using git commit -m "Initial commit"

Linking the project folder with Git so it is now under version control

At this point, we have created an R Project and have now linked it to Git version control. The next step is to link this with GitHub.

Linking this project with GitHub

To do this, go to GitHub.com, and again, create a new repository:
1) Make sure the name is the exact same as your R project;
2) Do NOT initialize a README file, .gitignore, or license.

Creating a repository on GitHub that is named the same as your R project

Upon creating the repository, you should see a page like this:

Your new repository on GitHub containing code to push from the command line

You should see that there is an option to “Push an existing repository from the command line” with instructions below containing code on how to do so. In Git Bash or Terminal, copy and paste these lines of code to link your repository with GitHub. After doing so, refresh your GitHub page and it should now look something like the image below.

When you re-open your project in R Studio, you should now have access to the Git tab in the upper right quadrant and can push to GitHub from within R Studio any future changes.

You’ve now pushed your R project repository to your GitHub repository of the same name

Working on an existing GitHub repository

If there is an existing project that others are working on that you are asked to contribute to, you can link the existing project with your R Studio. It follows the exact same premise as that from the above where you created a GitHub repository and then cloned it to your local computer using R Studio. In brief, in R Studio, go to File > New Project > Version Control. Select Git as your version control system, and like above, provide the URL to the repository that you are attempting to clone and select a location on your computer to store the files locally. Create the project.

Follow the same steps as previously done to clone your own repository to a new project in R Studio
Clone an existing project from GitHub from within R Studio

All the existing files in the repository should now be stored locally on your computer and you have the ability to push edits from your R Studio interface. The only difference from the above is that you did not create the original repository, instead you cloned somebody else’s.

Summary

In this lesson, we linked Git and R Studio, so that R Studio recognizes you are using Git as your version control software. Following that, we linked R Studio to GitHub, so that you can push and pull repositories from within R Studio. To test this, we created a repository on GitHub, linked it with a new project within R Studio, created a new file, and then staged, committed, and pushed the file to your GitHub repository!

We also went over how to convert an existing project to be under Git version control using the command line. Following this, we linked your newly version-controlled project to GitHub using a mix of GitHub commands and the command line. We then briefly recapped how to clone an existing GitHub repository to your local machine using R Studio.

Comments

Popular posts from this blog

Introduction to R Markdown

The Beginner’s Guide to R Markdown! We’ve spent a lot of time getting R and R Studio working, learning about Functionalities of R Studio and R Packages - you are practically an expert at this! There is one major functionality of R/R Studio that we would be remiss to not include in your introduction to R -  Markdown! Functionalities in R Studio Introduction to R Packages What is R Markdown? R Markdown is a way of creating fully reproducible documents, in which both text and code can be combined. In fact, these lessons are written using R Markdown! That’s how we make things: bullets bold italics links or run inline r code And by the end of this lesson, you should be able to do each of those things too, and more! Despite these documents all starting as plain text, you can render them into HTML pages, or PDFs, or Word documents, or slides! The symbols you use to signal, for example,  bold  or  italics  is compatible with all of those formats. Wh...

What is Data? And What is Data Science Process?

The Beginner’s Guide to Data & Data Science Process About Data: In our First Video today we talked about Data and how the Cambridge English Dictionary and Wikipedia defines Data, then we looked on few forms of Data that are: Sequencing data   Population census data ( Here  is the US census website and  some tools to help you examine it , but if you aren’t from the US, I urge you to check out your home country’s census bureau (if available) and look at some of the data there!) Electronic medical records (EMR), other large databases Geographic information system (GIS) data (mapping) Image analysis and image extrapolation (A fun example you can play with is the  DeepDream software  that was originally designed to detect faces in an image, but has since moved on to more  artistic  pursuits.) Language and translations Website traffic Personal/Ad data (e.g.: Facebook, Netflix predictions, etc.) These data forms need a lot of preprocessin...

Introduction to Functions and Arguments in R Programming: Part 2

The Beginner’s Guide to Functions in R Programming: Functions are an essential part of programming, and they play a critical role in R programming. In R, a function is a set of instructions that perform a specific task. Functions in R can have several arguments, and their evaluation can be lazy or eager. In this blog post, we will explore functions in R, including their  "dot-dot-dot" or ellipsis  argument, lazy evaluation, and more . Ellipsis or "dot-dot-dot" Argument in R Functions The "dot-dot-dot" or ellipsis argument in R programming is a special argument that can be used in functions to represent a variable number of additional arguments that are not explicitly defined in the function. The ellipsis argument is represented by three dots ... and is typically used at the end of the function's argument list. When the function is called, any additional arguments provided by the user after the defined arguments are collected by the ellipsis argument an...