The way that we interact with R and RStudio is slightly different to much statistics software.
Here, we will do a brief tour of R and RStudio.
By the end of the class, you will:
Know what all the panels of RStudio are for and what they mean
Navigate around the panels and explore their contents
Write code in the Source panel and send it to the Console panel.
Trouble-shoot some of the common errors when using R.
If you open R only (i.e., not RStudio), you will see something like the following.
Fig. 1. This is the Windows version from a long, long time ago
This basic, bare bones GUI features the R console and not much else.
For this course, we will use RStudio, a program that sits on top of R, and makes a slightly more user-friendly experience.
When you open RStudio, this is what you will see.
Fig. 2. The RStudio GUI
What is it and what does it mean?
There are four windows or panes.
They may be in a different order to here. You can rearrange them under the tabs: Tools > Global Options > Pane Layout.
You can also change their shape similarly to other windows by clicking and dragging, or maximising and minimising.
You can also change the colour scheme as well as text highlighting under: Tools > Global Options > Appearance.
Fig. 3. What each pane of the RStudio GUI is
The basic pattern of work is that you write your code in the Source (1), it is passed to the Console (2), where it is evaluated (i.e., run) by R. The Environment (3) and Files (4) windows provide other information or display plots and other outputs.
Fig. 4. RStudio with the Source pane highlighted
The source is the notepad or editor where you write your code, or R scripts.
You can open and save as many R scripts as you like.
Writing and saving a separate file allows you to keep a record of the commands and analysis that you did, rather than try and remember what buttons you clicked, or what code you typed into the R console.
This way of working also allows you to re-use code again and again. You could maybe keep a code library.
Note: Keeping records of your data analysis is good practice. Many (natural science) journals now recommend that you upload your data analysis code along with the article.
Note: Files of R code, or R scripts, are usually indicated with a “.R” file extension. If you double click on a .R file, RStudio will automatically open with it. However, you can also work with “.txt” files. If you cannot see the file extensions, you may want to change this option in Windows/Mac.
Note: You do not need to write you code in the RStudio Source window. You can open R script in any text editor, e.g., Notepad, BBEdit, gedit, … Using a Word Processing (e.g., MS Word) program is not recommended. This is because word processing software do not create not plain text documents, they often contain other code that is hidden, such as typesetting commands or code in binary that the program needs to work. Any work you submit as a word processor document will be returned without review.
Once you have written your code you need to move it from the RStudio Source Editor.
Copy from the Source and paste into the Console. The cursor then is in the Console pane, and you will need to click back in the Source pane to continue editing.
Highlight the code, click “Run” in the top right corner of the Source pane. The cursor remains in the Source pane
Highlight the code and use a keyboard shortcut: “Control + Enter” (Windows) or “Command + Return” (Mac).
Note: You can toggle back and forth between the Source and Console with “Contol + 1” (Source) and “Control + 2” (Console).
Fig. 5. RStudio with the Console pane highlighted
The Console is actually R, where your commands are run (or evaluated).
The arrow symbol (“>”) tells you that R is ready for new code.
If you type “2 + 2” directly into the Console and press Enter, R will evaluate that code immediately and return the output.
In the other command in the image (X <- 1:10), we have created the object X.
There is no output, because there is no answer to show. R stores ‘X’ in its memory, which you can see in the Environment pane.
Fig. 6. RStudio with the Environment pane highlighted
The Environment tab shows you all the objects (e.g., X) that are currently in your R session.
It also displays a short summary of each object.
The buttons at the top can be used to import data … but we will not do this, and we will always import data using R code.
The History tab shows all the code that has been entered into the Console in this session.
You can scroll back through the code to find something if you need to. You will probably not ever use this.
Fig. 7. RStudio with the Files pane highlighted
This pane is more useful than the Environment/History pane.
The Files tab is the equivalent of Explorer or Finder. It shows the file directory structure on your computer’s hard drive. You can use it to navigate through to specific directories to read and write files. You can also use it to set the working directory.
The Plots tab displays all the plots created in that session. You can scroll backwards and forwards through them, and export using the button (of course, I recommend that you export using a command instead!)
The Packages tab allows you to load packages in a point-and-click fashion rather than with commands. It is nice in that you can see all of the available packages currently on your computer.
The Help tab displays the R help files. You can search using the search box, or from the Console.
The hash or pound symbol (“#”) is used in R to indicate a “comment”, or a line in the file that is not evaluated by R when it is pasted in to the Console.
You can use comments to describe what you code is doing so that in 6 months time you can still understand what you meant!
You can also use comments to “comment-out” a line of code that you want to keep in the file but not run.
You will come across many instances where the data you display in the R console are too big for the window and over-run a single line.
It will always display  for the first element.
Then, R tells you how far along that sequence of data you are when the next line starts.
The arrows (>) tells you that R is ready.
The plus sign (+) tells you that R is waiting (very patiently) for something else from you …
Normally you will need to close out a function.
In any case, you can press ESC to terminate that code, which will not run, and return to the arrow prompt.
Spelling, grammar and syntax are very important in R. And also very frustrating.
R is also case-sensitive.
Most of the errors you will make will most often be due to errors of mispelling in one way or another.
Always make sure your grammar is safe
Here, R cannot find the function command that you have written.
Either it is not loaded, or most likely, you have mispelled it.
Here, the object name that you have typed in is not recognised.
Please check and try again!
White space is great to make your code more readable by people.
White space is not permitted in the names of functions or objects.
Commas are used to separate arguments in a function or elements in a vector (more later).
If they are missing, R will not know what goes where.
Periods are read as decimal points in numeric data (e.g., 398.2) or as text in the names of objects or functions (e.g., my.function).
Of course, different countries also use commas and periods differently.
In Latin America, commas are frequently used to indicate decimals, but this issue can be easily resolved when importing data (more later).
Almost everything in R can be put into one of two groups: objects or functions.
An object cannot be a function.
A function cannot be an object.
A Venn Diagram of objects and functions in R
An object is a thing.
This thing could be a single number, a sequence of numbers, a dataset, the output from a statistical test, or any thing …
Functions are tools, procedures, or commands.
They do stuff to objects.
Often you will take an object (e.g., a sequence of numbers), do something to it with a function (e.g., the mean() function), that function will output a new object (the mean of that sequence).