GitHub, Functions and Iteration
Today, we will start by setting up a basic workflow with GitHub. This workflow will make it easier to keep track of your work and collaborate with others. From there, we will learn about functional programming in R. This is one of the most important parts of coding with R (and I cannot stress enough how important it is). Built-in functions and packages can only get you so far. There will be times when it is both easier and more efficient to create your own.
This lab will teach you how to:
- setup a basic workflow with R Studio and GitHub
- write your own functions
- iterate functions over multiple inputs
- vectorise your functions using the purrr package
1 . Git and GitHub (Condensed Version)
This is the condensed version of the version control extra session, which can be found in full here. We will create your first repo on GitHub and see how we can use it to keep a record of all the changes that were ever made to your code. This is an essential component of collaborative coding efforts, but can also be immensely beneficial for your own solo projects.
The structure of this session is as follows:
- Create a new repo on GitHub and initialize it
- Clone this repo to your local machine
- Make some changes to a file
- Stage these local changes
- Commit them to our Git history with a helpful message
- Pull from the GitHub repo just in case anyone else made changes too (not expected here, but good practice).
- Push our changes to the GitHub repo.
Before we get into the nitty-gritty of this week’s session, I would like to suggest you all download GitHub Desktop. It is a GUI that lets you interact with GitHub and might be an additional option if your do not want to rely on RStudio or the Command Line alone.
There are a number of different GUIs that you can try out and play around with. Some are better than others. An example of another free GUI like GitHub Desktop is GitKraken.
Suggested Workflow 🏄
This is the recommended workflow that you should employ when you work with Git. You can see it as a sort of recipe that you should follow under most circumstances. At each stage you can find the instructions for working through both the Command Line and through RStudio. Be aware however that these are separate processes that should not be mixed. Either you use the shell for version control or you use RStudio or you use GitHub Desktop.
1. Create a new repo on GitHub
Go to your github page and make sure you are logged in.
Click green “New repository” button. Or, if you are on your own profile page, click on “Repositories”, then click the green “New” button.
How to fill this in:
- Repository name: my_first_repo (or whatever you want).
- Description: “figuring out how this works” (or whatever, but some text is good for the README).
- Select Public.
- YES Initialize this repository with a README.
- For everything else, just accept the default.
Great, now that you have created a new repo on GitHub, it is important to note that you should always create a repo prior to starting your work in RStudio.
2. Clone it to your local machine
Whatever way you plan on cloning this repo, you first need to copy the URL identifying it. Luckily there is another green button “Code” that allows you to do just that. Copy the HTTPS link for now. It will look something like this https://github.com/your-git-username/my_first_repo.git.
Using GitHub Desktop
Here is a short gif on how to clone a repo with GitHub Desktop: