RStudio tutorial

Introduction

We have likely already gone through an overview of what RStudio is and how it works with R but just in case you need a refresher on it you can read through the content of this webpage.

RStudio is what is called a Integrated Development Environment (IDE) - these are software suits that allow computer programmers to interact with the language which they are writing in. There are several IDEs for the R language but the most popular one is RStudio. RStudio is great because it comes with many extra features preinstalled or easily installable which you may find useful later on. These include the ability to make Shiny Web Apps, interactive documents, fantastic slide shows, websites and even publish books.

This tutorial will walk you through installing R and RStudio and then give you an overview of the features of RStudio.

Installing R

The first thing we need to do is install R. The easiest way to do this is to head over to the CRAN Cloud Mirror and click “Download R for insert your OS here”. Save this file to your computer and then run it. It shouldn’t take very long to complete.

Installing RStudio

RStudio is an interface which we will use to interact with R. You will need to have R installed already so make sure you do. Once R is installed then you can download and install RStudio by selecting the option relevant to you from the list available here.

The RStudio interface

Now that R and RStudio have been installed on your computer we can open up RStudio - do this now. The RStudio interface is about as close as you can get to a native GUI for R. The basic RStudio window is broken up initially into three panels. These are:

  1. The console - where you input code and text outputs appear
  2. The environment - where a list of all the variables, objects, datasets and functions are stored
  3. The file viewer and output - where you can navigate to subfolders and find relevant files. Also where graphical plot outputs are kept once you have generated them.

Getting the most out of RStudio

RStudio is a great programme. One of the reasons for this is that it works using projects. Think of projects as exactly that - a task or assignment that you are working on. Projects can be very small or very big. For instance - each practical that you are working on (although each one is relatively small) is a project. But a thesis (which may consist of many datasets and analyses) is also one project. This means that for every project that you do in real life you should also have a virtual RStudio project to match it. RStudio projects are stored in directories (the fancy name for a folder) and all the files related to that project (such as datasets and script files) are then kept neatly in that folder. When you start working on your project RStudio will store all of your variables and objects in the .RData file. The .Rproj file is there to link all aspects of your project together within RStudio. Working with projects is a great way to keep your work organised. It also makes it easy to come back to a project after a long time because nothing will get lost. And it also makes it easy to share your project with another person if you need to - all you need to do is copy the folder which contains your project and pass it on to someone else and then they can run the files (provided they have R, RStudio and all the packages that you have used).

It is a good idea to keep all of your projects in the same place. I tend to work with OneDrive and so I have created a folder called “R” in my OneDrive. This means that I will never loose my projects. Alternatively, you could create an “R” folder in your My Documents folder and then in that folder you can create another folder called “Projects” and in there is where you can store your projects. Set up this file structure now.

Suggested file structure
-My documents > R > Projects > Unique files for your projects
-OneDrive > R > Projects > Unique files for your projects

Back to the windows. There is a fourth window that will appear when you call up datasets or when you work with scripts. Let’s go through how to import data and what scripts are.

Working in scripts

If you would like a recap on scripts you can go through the tutorial from Hands on Programming with R Part 1.2.6.