Tutorial on connecting R projects to Github

YaLinChen (Amber)
5 min readMay 2, 2024
Source: Unsplash

It is valuable to use Github for better version controls and share the codes with people. Here, we will walk through the steps from creating a new R project to connecting it to Github.

Creating an R project

Go RStudio > File >New Project.

Select “New Directory”.

Select “New Project”.

I choose to create a project in the folder “R projects” and name it as MediumDemo.

After the creation, you should see .Rpoj in the MediumDemo directory.

Creating files in R Project

Go to RStudio > File > R Script.

Save an example R script in the directory. I name the file demo.R

Using Git Version Control

First, we go to RStudio > Tools > Project Options

Click “Git/SVN” from the left panel and select “Git” from the dropdown menu. Click Yes and Yes and restart RStudio.

Now you should see the Git tab on the upper right panel.

Click the tab and it should show the demo.R file. (You can ignore the two .xxx files)

Now, we want to commit the demo.R to upload it to Github. Select the file > Commit.

Type in Commit message and click “Commit”.

You should see similar message and you can Close this window and the Review Changes window now.

Now, we should not see demo.R in the panel since all new changes have been committed to Github.

Connecting to Github

Nest, we go to Github (https://github.com/). You will need to create an account if you do not have one yet. It is recommended to register with school accounts if possible.

After logging in/registration, we go to “Repositories” and click the “New” green button.

Type in “Repository name”. Add some description if you may. I want to keep this repository “Public” and you can also choose it to be “Private”. For now, let’s not Add README. Click “Create repository” green button.

You will see the following information. Copy the three git commands in the red bracket and paste them to Terminal in RStudio.

You may see some message like this if you are a Mac user.

When you encounter this, press the keys Option and Space and type keychain. Click “Passwords” panel and click on the github.com you are logged in. Double click on the item.

Click “Show password” and copy the shown password to the message above.

After it is allowed, you should see similar messages in Terminal in RStudio.

Now, you can refresh the Github page of the repository and you will see the below page.

Adding README and license

Now, we will learn to add README.md.

Go to RStudio > File > New File > Markdown File.

Type in information such as

## README

<CC BY license. See below>

This is the start of README.

<CC BY license. See below>: CC BY license is commonly used. “This license requires that reusers give credit to the creator. It allows reusers to distribute, remix, adapt, and build upon the material in any medium or format, even for commercial purposes.”

To get the license, go to https://chooser-beta.creativecommons.org/.

> select “Yes, I know the license I need”, “Next”
> select “CC BY 4.0”, “Next”
> at the right, click HTML tab, copy the code and paste to the position <CC BY license. See below>
> save the file as README.md

Now, you should have README.md in the project folder.

We will now commit the change and push to Github.

On the Git panel, select README.md > Commit

Type Commit message and click “Commit”

Close the pop-up window and click on “Push” at the upper right corner.

Now, we go to Github browser and refresh. And there! You will see the README.md with CC BY license.

I hope this helps you get your GitHub projects off the ground 🏋️‍♀️

--

--