Skip main navigation

Getting started with RStudio

What are some important tips for RStudio and some R commands? Looking at how to start RStudio, RStudio scripts, loading data.

Watch the previous video called Demonstration: Basics of RStudio for data summaries before working through this and the next step. It gives an overview of the statistical software R and its graphical user interface RStudio. In particular, we provide you with the R code behind selected examples used in the previous steps.

The R code blocks shown in this and the next step are not interactive. You can copy and paste them into RStudio and execute them and see the results.

You can find instructions on how to download a copy of RStudio in the zip folder provided in the ‘Downloads’ section below. Here you can also find an R file version of all R codes used in this reading.

1. Starting RStudio

The opening screen of RStudio will look like this:

The opening screen of R Studio.

It is similar to plain R (so far).

You can start typing R commands in the Console pane (bottom left). Press Enter to execute the commands one by one:

x <- c(3,1,4,1,5,9,2,6) 
print(x) 
plot(x, pch=16, col="red")

The right side of the screen is the Plots pane for plots and graphs. You will need to select the Plots tab to see this pane:

Plots pane in R Studio.

Hint: on the assignment symbol.

Although the equality sign = can be used in place of the assignment symbol <- (less than, minus), it is advisable to use the latter. This helps to clarify your R code and avoid confusion because = (equals) is also used to specify various options in R commands.

2. RStudio scripts

To leverage the nice functionality of RStudio, it is preferable to work with an R script containing a set of commands and comments.

To create a new script inside RStudio, click on the New File icon in the upper left of the RStudio toolbar; it looks like a white plus sign in a green circle.

In the opening menu, click R Script, and an empty script will open in the Editor pane (top left):

The Editor pane (top left) in R Studio.

Alternatively, a new script is created by clicking File > New File > R Script in the top menu.

You can type (or copy and paste) your R commands in the Editor pane. Comments can be added by using the hash symbol #.

Commands are executed by clicking Run in the Editor toolbar (while keeping the cursor somewhere on the command), or simply by pressing Ctrl + Enter.

You can even execute blocks of commands by highlighting them first with the mouse:

Highlighted command block ready for execution.

You can save your script by clicking the Save icon in the top RStudio toolbar and choosing a name and location for the file (it will have the extension *.R).

Hint: on keeping your RStudio workspace tidy

When you quit RStudio, do not save the workspace to an  .Rdata  file; likewise, when you launch RStudio, do not reload the workspace from an  .Rdata  file. It is advisable to turn this feature off and clear R’s memory at every restart.

Starting with a blank workspace ensures relevant feedback and encourages the development of complete and self-contained scripts.

In RStudio, set this via Tools > Global Options, making sure to clear “Restore .RData into workspace at startup” and choosing Never on the “Save workspace to .RData on exit”.

To load an existing R script to RStudio, click File in the RStudio toolbar and find the file you want to load, then click on it.

To do the same outside of RStudio, find the required R script file on your computer and double click on it – this should open RStudio automatically, with your script file appearing in the Editor pane.

Hint: on opening an existing R file

It is easy to open a recent R script:

  • Use the shortcut Ctrl + O.
  • Go to the horizontal toolbar at the top of the RStudio screen. Select on the small black triangle next to the icon of an open file with a green arrow pointing to the right (reads Open an existing file when you hover your mouse over it).

Choose a required file in the opening menu (which will show only  *.R  files).

3. Loading data

Small datasets can be directly loaded to the RStudio session as a list of observations, for example:

x <- c(3,1,4,1,5,9,2,6)
x[1] # 1st component of vector x
## 3 
max(x) # maximum component of vector x 
## 9

If the data has more structure (e.g it is a table with several columns), you can input it as a data frame.

For instance, this is how we can load the women’s clothing size dataset considered in Example 1 of the reading Case studies: Inference from data:

# Data frame: women's clothing sizes 
f <- data.frame(c(6,8,10,12,14,16,18,20,22,24,26), 
                          c(1,11,17,29,31,27,17,13,9,3,2)) # two columns of values 
colnames(f) <- c("size","freq") 
head(f) # to see the first few rows 
## size freq 
## 1 6 1
## 2 8 11
## 3 10 17
## 4 12 29
## 5 14 31
## 6 16 27

The command  colnames()  is used to give names to the columns.

We can refer to a particular column with the aid of the dollar sign $, for example:

f$size 
## 6 8 10 12 14 16 18 20 22 24 26 

Alternatively, we can use the command  attach() to inform R of the column names, so they can be referred to directly:

attach(f)
print(size) 
## 6 8 10 12 14 16 18 20 22 24 26 

In order to not confuse R, it is recommended to detach the column names at the end of the session:

detach(f) 

Data may also be loaded as a file with tabular data stored in plain text.

If uploading from a local drive, first, set a working directory, then read the file, for example:

setwd("E:/Teaching/Data") 
f1 <- read.csv("clothing.csv", header=TRUE) 
head(f1) 
## size freq 
## 1 6 1
## 2 8 11 
## 3 10 17
## 4 12 29
## 5 14 31
## 6 16 27

Note the use of forward slash  (/)  in the directory path, and the quotation marks around the file name. The option  header=TRUE  keeps the column names.

The data file loaded here is just an Excel text file with extension  *.csv  (‘comma-separated values’):

Excel text file.

Alternatively, we may be able to download data from the internet, for example:

# TV hours watching per day (data from GSS) 
gss <- read.csv( "https://raw.githubusercontent.com/artofstat/data/master/Chapter2/TVhours.csv") 
head(gss) 
## tvhours 
## 1 0 
## 2 0 
## 3 0 
## 4 0 
## 5 0 
## 6 0

The link used here is from the resources website of the book by Agresti, A., Franklin, C., Klingenberg, B. 2023. Statistics: The Art and Science of Learning from Data, Pearson. This data is sourced from the General Social Survey (GSS) database, hosted by the University of California at Berkeley.

This article is from the free online

Statistical Methods

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now