How to Create R Packages and host it on GitHub

Create R Packages with RStudio and GitHub

Do you have R code sitting on your laptop? You don’t know what to do with it? Create a package share it with the world!!

By Fahad Usman

Creating R packages and hosting on GitHub is super easy (if you know what you’re doing). I spent ages and it was frustrating for me to get started quickly with RStudio and GitHub.

What do you need?

  1. Have a GitHub account
  2. Install GitHub on your computer
    1. Windowshttp://git-scm.com/download/win.
    2. OS Xhttp://git-scm.com/download/mac.
    3. Debian/Ubuntusudo apt-get install git-core.
    4. Other Linux distros: http://git-scm.com/download/linux.
  3. Install RStudio

Git comes pre-installed on Macs (How awesome is that!!)

GitHub is a web-based hosting service for version control using Git. It is mostly used for computer code. It offers all of the distributed version control and source code management functionality of Git as well as adding its own features.”

Now Goto GitHub and create a new repository (repo). Name the new repo same as your R package.

Creating your package in RStudio

Fire up your RStudio which you just installed.

Now click File -> New Project -> R Package

Enter the package name exactly as your github repo you created earlier.

Click the tick box "Create a git Repository" and click ok.

RStudio will now create a new project.

Creating ssh keys for GitHub to avoid entering username / password every time you push changes up to GitHub

Click Tools -> Global Options ... button

Now select Git/SVN Item and click create RSA key... button.

This will generate your public and private key. You can read more about this type of encryption in my THIS post.

Once generated. Click "view public key" and it will show you your public key which you need to upload to GitHub.

Now go back to https://github.com/settings/keys

Click "New ssh key" button and give it a name and copy your ssh key that you created in previous step. I would recommend adding your ssh key here. The benefit is that you will not have to enter username password every time you push your updates back to Github.

Pointing RStudio to your online (github) Repo:

Now you need to tell RStudio where to push changes to on GitHub.

Go back to GitHub (Hopefully you are still logged in), select the repo you created.

Click the green button "clone or download" select the option "use ssh" (It’s a link on top right corner)

Copy this link and go back to your RStudio.

Initiate the upstream tracking of the project on the GitHub repo

In the console window, click on “Terminal” tab and Enter:

git remote add origin (paste the link here you just copied above from github repo, it will look like [email protected]:your_username/repo_name.git

Once you executed that, then confirm by clicking on the Tools menu -> Project Options then select Git/SVN option on the left menu items and you should see your repo there.

 pull all files from the GitHub repo (typically just readme, license, gitignore)

git pull origin master

Note:This is vitally important because if you don’t pull first and make changes, it won’t let you push those changes back to github saying your version is not as same as the master version on GitHub hub. I give up here and started again

Set up GitHub repo to track changes on local machine

Now in RStudio, you should see Git tab and Git addon being added. click on git tab, and you will see list of files with "?" mark meaning these files are not being tracked. Just click them and the “?” will turn to "A" meaning these files are now added to be tracked. Now click the "commit" button and add a short message about your first commit. Now press the “Commit” button as shown:

Once done, you’re local repo has more edits/files then online. So you need to upload/push these to GitHub now.

Pushing changes back to GitHub:

within RStudio go back to the terminal and type:

git push -u origin master

it will upload all the files etc. back to your GitHub which you can check by going back to GitHub repo via browser and refreshing the page. You should see all your files being uploaded…

That’s it! Enjoy coding 🙂

Leave a Reply

Close Menu