#R

March 12, 2018

Basic setup with R

This is my basic setup for using R.

If you follow this guide you will be able to collaborate with me easily.

I have kept things as simple as possible, this is more of a checklist than a detailed how-to guide covering every nuance. Here are the steps;

  • Install R
  • Install R Studio
  • Create a GitHub account
  • Install GitHub Desktop
  • Set up a project

Install R

Go here for Windows.
Go here for MAC.

You want to download a file named something like R 3.4.3

Now unpack and run that program to install R on your computer.

You can check your R installation by opening the Terminal on MAC or something similar in Windows.

Now type “R” (ignore the quotations) in the Terminal and hit ENTER.

You should see something like this;

(I found this post very useful to troubleshoot linking images)

From here you can play with anything in R that you might see in an online course, such as DataCamp. I strongly recommend using DataCamp to learn R.

This step is all that you need to do to use R.

Install R Studio

I recommend that you download R Studio, whichever version suits your computer.

Create a GitHub account

Version control is not just useful for working in teams - it will also help you retain some sanity as you create more complex scripts and projects. You are being kind to yourself by learning the discipline of version control.

So, go to GitHub and create an account. The basic subscription is free which is fine for now.

Install GitHub Desktop

I suggest GitHub Desktop because it works for me.
Install the relevant version for your machine and follow the prompts.

At this point, you need to think about where you will store your R work on your local machine.

You can use a default folder location, or I suggest creating a folder called “GitHub” or “Data Science” or similar that you can use to store your project folders.

Set up a project

Here how I set up a new project and store it on my local machine and GitHub.

These are the steps;

1. Create a new repository on GitHub

I suggest starting with a repo on GitHub first, it just seems much cleaner and easier for everything that follows. Go to your repositories tab and click the green “New” button, and give your repo a name.
Don’t use spaces in the name.
Do initialize the file with a README.
Do add a .gitignore - I suggest picking any .gitignore template - it doesn’t matter because we will change it soon.

2. Edit your .gitignore file

Your .gitignore will descibe which files do not get shared by version control. I use it to make sure large datasets on my local machine don’t get pushed to GitHub - there is a file size limit and you will get an error. Real programmers also use it for all kinds of other exclusions.

Click on the .gitignore file.
Click on the pencil next to the trash icon, so you can edit the file.
Delete everything and put in the following;

Click the green button that says “Commit changes”

3. Clone your repo from GitHub to your local machine

On the homepage for your new repo you will see a green button called “Clone or download”. Click it and select “Open in Desktop”, then something like “Open with GitHub Desktop app”.
Now you can choose where to store the local files.
I recommend keeping the file name the same, just choose your folder.

Now you have a folder on your local machine that is linked to your GitHub profile. It contains one visible file, your README, and one hidden file which is your .gitignore. You can also create your “data” folder now.

4. Create a Project in R Studio

Open R Studio, click “File” and “New Project”. Create your project in an existing directory. This existing directory should be the directory you just created on your local machine.

Now you can compare your local folder with the R Studio project window. Note that the hidden .gitignore file is visible in the R Studio window.

5. Create an R script

Create an R script (File -> New File -> R Script). Call it whatever you want and then save it into the same folder - you should now see it in the R Studio window and your file browser.

6. Push your changes to GitHub

Go back to GitHub Desktop and navigate to your examnple_repo. You should see there are some changes.

Write a comment about changes and then click “Commit to master”.

Then click “Sync” in the top right.

Then go back to your GitHub account - you should see that the example_repo has been updated and you will see your R Script file has been added to the remote repository.

What next?

If you have come this far, you are ready to play around with the many excellent online R courses but with the added bonus of a robust project-oriented file structure and an approach to version control for your work.

Kasia Kulma has an excellent post that you should read now. It has everything you need to know about how to set up and run your projects.

© Will Bidstrup 2018