How to upload your R code on GitHub: example with an R script on MacOS
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:
- a GitHub account
- the GitHub Desktop application installed on your computer
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](https://i2.wp.com/www.statsandr.com/blog/2020-05-24-how-to-create-a-github-repository-for-r-scripts-on-macos_files/upload-r-code-on-GitHub-example-with-an-r-script-on-mac-os-1.png?w=578&ssl=1)
Step 1: Go to github.com/login and sign in
![Step 2: Go to your GitHub profile and create a new repository](https://i1.wp.com/www.statsandr.com/blog/2020-05-24-how-to-create-a-github-repository-for-r-scripts-on-macos_files/upload-r-code-on-GitHub-example-with-an-r-script-on-mac-os-2.png?w=578&ssl=1)
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](https://i1.wp.com/www.statsandr.com/blog/2020-05-24-how-to-create-a-github-repository-for-r-scripts-on-macos_files/upload-r-code-on-GitHub-example-with-an-r-script-on-mac-os-3.png?w=578&ssl=1)
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](https://i2.wp.com/www.statsandr.com/blog/2020-05-24-how-to-create-a-github-repository-for-r-scripts-on-macos_files/upload-r-code-on-GitHub-example-with-an-r-script-on-mac-os-4.png?w=578&ssl=1)
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](https://i1.wp.com/www.statsandr.com/blog/2020-05-24-how-to-create-a-github-repository-for-r-scripts-on-macos_files/upload-r-code-on-GitHub-example-with-an-r-script-on-mac-os-5.png?w=578&ssl=1)
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](https://i1.wp.com/www.statsandr.com/blog/2020-05-24-how-to-create-a-github-repository-for-r-scripts-on-macos_files/upload-r-code-on-GitHub-example-with-an-r-script-on-mac-os-6.png?w=578&ssl=1)
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](https://i1.wp.com/www.statsandr.com/blog/2020-05-24-how-to-create-a-github-repository-for-r-scripts-on-macos_files/upload-r-code-on-GitHub-example-with-an-r-script-on-mac-os-7.png?w=578&ssl=1)
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](https://i2.wp.com/www.statsandr.com/blog/2020-05-24-how-to-create-a-github-repository-for-r-scripts-on-macos_files/upload-r-code-on-GitHub-example-with-an-r-script-on-mac-os-8.png?w=578&ssl=1)
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](https://i0.wp.com/www.statsandr.com/blog/2020-05-24-how-to-create-a-github-repository-for-r-scripts-on-macos_files/upload-r-code-on-GitHub-example-with-an-r-script-on-mac-os-9.png?w=578&ssl=1)
Step 7: Your new repo appears on the GitHub Desktop application
![Step 8: Open an R script and write your code](https://i0.wp.com/www.statsandr.com/blog/2020-05-24-how-to-create-a-github-repository-for-r-scripts-on-macos_files/upload-r-code-on-GitHub-example-with-an-r-script-on-mac-os-10.png?w=578&ssl=1)
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](https://i2.wp.com/www.statsandr.com/blog/2020-05-24-how-to-create-a-github-repository-for-r-scripts-on-macos_files/upload-r-code-on-GitHub-example-with-an-r-script-on-mac-os-11.png?w=578&ssl=1)
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](https://i2.wp.com/www.statsandr.com/blog/2020-05-24-how-to-create-a-github-repository-for-r-scripts-on-macos_files/upload-r-code-on-GitHub-example-with-an-r-script-on-mac-os-12.png?w=578&ssl=1)
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)](https://i1.wp.com/www.statsandr.com/blog/2020-05-24-how-to-create-a-github-repository-for-r-scripts-on-macos_files/upload-r-code-on-GitHub-example-with-an-r-script-on-mac-os-13.png?w=578&ssl=1)
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](https://i2.wp.com/www.statsandr.com/blog/2020-05-24-how-to-create-a-github-repository-for-r-scripts-on-macos_files/upload-r-code-on-GitHub-example-with-an-r-script-on-mac-os-14.png?w=578&ssl=1)
Step 11: Push the commit to github.com
![Step 12: On your GitHub profile, click on the newly created repository](https://i0.wp.com/www.statsandr.com/blog/2020-05-24-how-to-create-a-github-repository-for-r-scripts-on-macos_files/upload-r-code-on-GitHub-example-with-an-r-script-on-mac-os-15.png?w=578&ssl=1)
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](https://i1.wp.com/www.statsandr.com/blog/2020-05-24-how-to-create-a-github-repository-for-r-scripts-on-macos_files/upload-r-code-on-GitHub-example-with-an-r-script-on-mac-os-16.png?w=578&ssl=1)
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](https://i2.wp.com/www.statsandr.com/blog/2020-05-24-how-to-create-a-github-repository-for-r-scripts-on-macos_files/upload-r-code-on-GitHub-example-with-an-r-script-on-mac-os-17.png?w=578&ssl=1)
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](https://i2.wp.com/www.statsandr.com/blog/2020-05-24-how-to-create-a-github-repository-for-r-scripts-on-macos_files/upload-r-code-on-GitHub-example-with-an-r-script-on-mac-os-18.png?w=578&ssl=1)
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.
Get updates every time a new article is published by subscribing to this blog.
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.