Site icon R-bloggers

How to upload your R code on GitHub: example with an R script on MacOS

[This article was first published on R on Stats and R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Introduction

Few days ago, a colleague asked me how to upload some R code on GitHub in order to make it accessible to everyone. Due to the lockdown, I could not just go into his office and show him on his computer. So I sent him several screenshots showing, step by step, how to do so.

Right before I deleted the screenshots I’d just taken, I thought that perhaps they would be useful for other persons, so I wrote this article.

Note 1: The screenshots have been taken on MacOS and I have not tested it on Windows. Do not hesitate to let me know in the comments whether it is similar or not on other operating systems.

Note 2: There must be other ways to do it, but the method shown below is (in my opinion) easy and works well.

Prerequisite

In order to follow this guide and upload your R code on GitHub, you will need at least:

Step-by-step guide

For this guide, I use a R script created to plot the evolution of the hospital admissions due to COVID-19 in Belgium. See the repository on GitHub in case you want to see the final result before proceeding further.

You often hear

“A picture is worth a thousand words”

So here is without further ado how to upload your R script on GitHub in images:

Step 1: Go to github.com/login and sign in

Step 2: Go to your GitHub profile and create a new repository

Step 3: Set the name, description, audience and README file for the new repository

Be careful that if you choose to make it public, it will be visible to everyone. If you do not want to share the code, but still want it to be uploaded on GitHub, choose the private option.

Initializing the repo with a README file is not mandatory, but I strongly recommend it so you will be able to add information for this repo.

Step 4: You now see your new repository with only the README file

Step 5: Open the GitHub Desktop application and sign in with your GitHub account

Step 6 a: Clone the repository you just created on github.com to your computer

Step 6 b: Clone the repository you just created on github.com to your computer

Step 6 c: Clone the repository you just created on github.com to your computer

Step 7: Your new repo appears on the GitHub Desktop application

Step 8: Open an R script and write your code

(See how to install R and RStudio if you are unfamiliar with it.)

Step 9 a: Save your R script in the corresponding folder

You can see the path to the folder in step 6 c, under “Local Path”.

Step 9 b: Save your R script in the corresponding folder

Step 10: Reopen GitHub Desktop, edit the commit title (1) and click on the commit button (2)

In step 10, make sure that the current repository (see top left) is the repo you are currently working on.

Step 11: Push the commit to github.com

Step 12: On your GitHub profile, click on the newly created repository

Step 13: You now see your script which has been added to the repository

Your R code is now available online via the new repository on your GitHub profile. If you need to share it, you can simply share the URL of the repo.

Additional notes

If you need to edit your code:

Locate the folder of the repo on your computer, open the R script, edit it and save it

After your script is saved, do not forget to commit the changes and push the commit (see step 10 above).

In case you need to see an old version of your code, you can see all commits by clicking on the commits tab accessible via the repo on github.com:

See all commits

Thanks for reading. I hope this article helped you to create a GitHub repository, and upload your R scripts so that it is available to anyone.

As always, if you have a question or a suggestion related to the topic covered in this article, please add it as a comment so other readers can benefit from the discussion.

To leave a comment for the author, please follow the link and comment on their blog: R on Stats and R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.