How to use RStudio and Github for version control

Objective: Integrate Github and RStudio to maintain an R code project

Setup your Github account :

Github is the repository for your code. You can share the scripts written by you with everyone. Read more about it – https://en.wikipedia.org/wiki/GitHub .

Register at Github for an account – https://github.com/

Follow these steps to integrate github with rstudio.

Git Installation: 

Prior to using RStudio’s version control features you will need to ensure that you have Git and/or Subversion installed on your system. The following describes how to do this for various platforms.

Windows & OS X: http://git-scm.com/downloads

Debian/Ubuntu: sudo apt-get install git-core

Fedora/RedHat: sudo yum install git-core

An excellent resource for learning more about Git and how to use it is the Pro Git online book. Another good resource for learning about git is the Git Bootcamp provided by GitHub.

Subversion

OR

Windows: SilkSVN (or any of the other packages listed here)

OSX (≤ v10.7): Not required — already included in versions of OSX 10.7 and before

OSX (v10.8+): Install Xcode’s Command Line Tools from Apple’s developer downloads (requires free Apple Developer ID)

Debian/Ubuntu: sudo apt-get install subversion

Fedora/RedHat: sudo yum install subversion

An excellent resource for learning more about Subversion and how to use it is the Red Bean online book.

Getting Started

Once you’ve installed your preferred Version Control system, you’ll need to activate it on your system by following these steps:

Go to Global Options (from the Tools menu)

Click Git/SVN

Click Enable version control interface for RStudio projects

If necessary, enter the path for your Git or SVN executable where provided. You can also create or add your RSA key for SSH if necessary.

RStudio’s version control features are tied to the use of Projects (which are a way of dividing work into multiple contexts, each with their own working directory).The steps required to use version control with a project vary depending on whether the project is new or existing as well as whether it is already under version control.

Go to https://github.com/settings/keys

Click on 

Give a title (“my_key”) and paste the key that we copy from the R-studio.

Now go to

https://github.com/new

Repository name: myrepo (or whatever you wish)

Check public

Check “Initialize this repository with a README”

Click the green button “Create repository.”

It should look like this one.

Copy the HTTPS clone URL to your clipboard via the green “Clone or Download” button.

In the RStudio –

Click on the File -> New Project

Click on new directory –

Click on R package

Type the name R101 and click create project.

Now, in the RStudio –

Click on shell –

Type this command in this black box but replace “barupal” with your github username

git remote add origin https://github.com/barupal/R101.git

Then type this command –

git pull origin master

It will download the files from github repos to your local version. README.md file was downloaded.

Now type these commands in the black box window ( aka shell).

git config –global user.email “YourEmailID”

git config –global user.name “YourName”

Replace “YourEmailID” with email id that you have used for the github account and “YourName” with the your name.

In the RStudio, you can see that README.md file is downloaded that means that everything worked so far.

Now check staged for all the files in the git tab. And click commit.

Type “First commit” or whatever comments you want to add in the commit message box and click commit, it will update the git file on your laptop.

Results should be this. Click close.

Close the commit window.

Now in the Rstudio Git tab, Click on Shell again.

Type this command and press enter.

git push -u origin master

This will upload the codes and files from your local files to github repos. After this, the git tab in RStudio should look empty, which means all the updated files have been uploaded to the github repo. If you see any files in here, click commit, type a comment, and commit. Files that have been changed will appear here. After that, click on Push, it will do the syncing.

Now in the R-script file hello.r add these lines are the curly bracket.

hello1 <- function() {

print(“Hello, world!”)

}

Click save.

Now in the git tab, check the hello.r file and click on commit. Type any comment in the commit box and click commit.

Then, click on the Push button. This shall be the end result.

Fantastic ! you have achieved how to build an r package from scratch and maintain a repository on github.