Chapter 1: RStudio Refresher
Preamble
Learning Outcomes
The objective of this chapter is to introduce you to the RStudio graphical user interface (GUI) for R. This GUI will be used throughout this eBook as the environment for undertaking analyses.
After completing this chapter, you should have a basic understanding of the fundamentals of the RStudio environment, including creating and executing R scripts, monitoring progress and examining output.
RStudio is a mature working environment and we do not cover all features. As your skills develop you will be able to expand your knowledge beyond the basics presented here.
Prerequisites
As this is the first chapter, there are no specific prerequisites.
You may find it helpful to listen to a podcast summary of the introductory material in this chapter:
RStudio: Primer on the Interface (mp3)
Workflow
This chapter introduces R and RStudio, with a focus on the latter. The latest versions of both packages will need to be installed.
Because this chapter focusses primarily on the RStudio graphics user interface and the R programming environment provided by RStudio, you should follow the instructions in the chapter and where required, follow those instructions in RStudio.
There are also Exercises to stretch your capabilities. You will need to download the Worked Examples and Exercises to the RStudio Tutorial Pane, and associated data files to your local workspace.
Additional Reading
Many tutorials on RStudio can be found online, each with its strengths and weaknesses. To expand your knowledge and gain experience in advanced topics in RStudio you should select the option that best suits your tastes.
For a more comprehensive introduction to R programming, you might like to refer to the official documentation. There is also a useful cheat sheet put out by the developers of RStudio.
Session 1-1: Introduction to RStudio
What is R?

R is a statistical computing language based on an earlier implementation of a programming language called S available in commercial form. R is GPL-Licenced.
R was created by Ross Ihaka and Robert Gentleman (hence the name R) at the University of Auckland, New Zealand, and is now developed by the R Development Core Team.
Many statistical packages on the market, such as SAS, SPSS and Statistica are regarded as fourth-generation statistical programming languages. The R programming language is a hybrid between a third-generation language such as C or FORTRAN and a fourth-generation language such as SAS. This provides for much greater flexibility for the analyst but demands much more in terms of programming skills.
R supports a wide variety of statistical and numerical techniques, with comparable benchmark results to Octave and its proprietary counterpart MATLAB. R also provides the analyst with a very wide range of packages, which are user-submitted program libraries, for specific functions or specific areas of study. As a result, R is one of the most comprehensive statistical analysis systems on the market. R has exceptionally good graphical capacities and can be used to produce publication-quality graphs.
The library we will be primarily using is dartRverse, a collection of scripts to facilitate analysis of Single Nucleotide Polymorphisms (SNPs) generated by commercial providers such as Diversity Arrays Technology Pty Ltd (DArT). Although dartRverse has some unique analyses, it is primarily for data manipulation, exploratory analysis, and a conduit to other packages used for SNP analysis.
To work through the chapter in this eBook, you will require familiarity with the R GUI, RStudio, but you will not necessarily need to be well versed in R programming.
What is RStudio?

RStudio is an integrated development environment for R. It provides a robust set of tools to help you write and execute R code efficiently.
Here are some features of RStudio:
- Code Editor: RStudio provides syntax highlighting, code completion, and other powerful editing tools for R.
- Interactive Console: You can write and execute R commands directly in the interactive console, allowing for immediate execution and feedback.
- Plotting and Visualisation: It integrates with R’s plotting capabilities and provides a window for viewing plots and graphs created with R.
- Package Management: RStudio provides a user-friendly interface to manage R packages, helping to install, update, and manage the libraries you need.
RStudio is available as a free open-source version that can be installed on Windows, macOS, and Linux.
Whether you are a beginner or an experienced R programmer, RStudio offers a comprehensive set of tools to make your work with R more productive and enjoyable.
If you are well versed in RStudio, you can skip to the section Accessing Worked Examples and Exercises.
The Command Line Interface
When you first start RStudio, a graphical user interface opens with many features to assist you (Figure 1-1). After an introductory text appears in the Console, a Command Prompt is presented (>), and the system awaits instructions.

Before we move on, there are a couple of little tricks worth mentioning. The first is that the Console can be cleared of text using Ctrl+L (^L). The second tip is that the up-arrow will recall previously submitted commands, which will save you a lot of typing. Try these as you go along.
The simplest way of using R is to supply instructions to the console.
sum <- 125 + 172
Here we are adding two numbers and putting the answer in the scalar object (single-valued vector) called sum.
You can view the contents of an object simply by giving its name in response to the command prompt.
sum
[1] 297
| Try some assignment statements for yourself to undertake some arithmetic. |
beetles <- c(15.2,12.1,17.8,13.9,16.4,15.1)
There is a lot to this simple command. What we are doing here is creating an ordered set of values, referred to as a vector in R terminology. In this case, the data are lengths of beetle elytra. The concatenate function c() is used to create the vector which is then assigned to the object beetles using the assignment operator <-. The object beetles is called an object because it is a self-contained entity with associated attributes that are recognised in subsequent calculations.
Again, you can view the contents of an object simply by giving its name in response to the command prompt.
beetles
[1] 15.2 12.1 17.8 13.9 16.4 15.1Instructions to the command line are terminated with a return (↵) or a semi-colon (;). R instructions are case-sensitive, so the objects beetles, Beetles and BEETLES are all considered separate objects. It is wise to adopt a consistent practice, such as always using lowercase unless uppercase is demanded by the R syntax.
Spaces matter, sometimes. You will need to watch that.
If you instruct R to undertake some action and do not assign it to an object, then R will direct the results of the instructions to the screen. For example, requesting R to create the vector of beetle elytra without assigning it to the vector object beetles will result in the contents of the vector being listed on the screen.
c(15.2,12.1,17.8,13.9,16.4,15.1)
[1] 15.2 12.1 17.8 13.9 16.4 15.1
| Try some assignment statements again, but this time directing the results to the screen. A bit like a simple calculator. |
As an object, beetles can be used in subsequent calculations. For example, if you want to calculate the mean of the values in the vector beetles:
mean(beetles)
[1] 15.08333
R programs often comprise a series of nested instructions, and the same result could have been obtained by using
mean(c(15.2,12.1,17.8,13.9,16.4,15.1))
[1] 15.08333
This is the advantage of an object-oriented approach to programming.
The Graphics Interface
When a command requires more sophisticated output, R will open a purpose-built window. The most useful of these is the graphics window.
A scatter plot of 1000 pairs of coordinates drawn at random from a bivariate standard normal distribution (mean=0, stdev=1) is made by combining the plot() function with the rnorm() function as follows:
plot(rnorm(1000,0,1),rnorm(1000,0,1))
The output will appear in the graphics window (Figure 1-2).
| Run this command to see if you can replicate the output below. |
This scatter plot can be saved to a file or copied to the clipboard by right-clicking on the graphics window and choosing the desired outcome.

You can pull the graphics out into its own window with the [Zoom] tab, or export the image in one of the standard formats using the [Export] tab.
The Editor Interface
Using the Command Line Interface is great for a quick analysis, but it is essentially a calculator mode. Once you have done the calculations, you walk away only with the results. In more substantial analyses, we need to better manage the set of programming instructions needed to do the job. We do this using the R editor, which can be accessed from the file menu
File > New File > R Script
or by typing
Ctrl+N (^N)
| Open the RStudio Editor. |
The idea is to type all instructions in the RStudio editor for progressive submission or for submission as a block. At the end of the process, you have a complete program listing that can be saved to disk for later use.
The R editor is not all that sophisticated. Each instruction is typed in on its own line. A line can be submitted for execution by placing the cursor on it and typing Ctrl+Enter (^↵). Alternatively, blocks of instructions can be highlighted and submitted in the same way. This allows progressive debugging of the program as it is constructed.
It is wise to include abundant comments as part of your programs, so that you can understand them later or pass them to others in a comprehensible form. Comments are preceded by the # character and terminated by a return (↵).
Our simple R program, with comments added is shown in Figure 1-3.
# Creating and displaying a list of beetle measures
beetles <- c(15.2,12.1,17.8,13.9,16.4,15.1)
beetles
mean(beetles)
Type the above script into the RStudio Editor. Highlight the text and submit it for execution using Ctrl+Enter (^↵) |
Accessing Packages
Not many users of R program all the scripts that they require to undertake a task. This would be like reinventing the wheel. Instead, it is possible to access scripts written by those who have come before you, and who have made those scripts available as a package. A complete list of available packages can be obtained from the Comprehensive R Archive Network known as CRAN.
You will generally identify the packages you require after some investigative work on the web, by talking to colleagues or taking pointers from the literature. For example, the package reshape2 is useful for rearranging data, and can be accessed using the RStudio menus (Packages > Install Packages) or using the statement
install.packages("reshape2")
| Now you have a go. Install reshape2 |
You may need administrator rights to install packages. Packages are only installed once, not every time you require them.
The directory where packages are stored is called the library. R comes with a standard set of packages. A list of installed packages can be obtained using
library()
or by examining the list using the Packages menu.
Apart from those included in the standard implementation of R, packages are, once installed, loaded for use in a session with the library function.
library(reshape2)
| Now that you have installed the package reshape2, load the package |
Packages are installed only once, not every time you use them.
Error Handling and Help
When you make an error in the syntax of commands given to R, the program will respond with some form of diagnostic message. Sometimes these are self-explanatory, sometimes they are not.
Command line help
Fortunately, R has very extensive help documentation. If you know the exact name of the function you want help with (e.g. hist for plotting histograms), help can be obtained using
?mean
A window is displayed with help on the R function to generate the arithmetic mean. Note that the help gives you a list of possible parameters to pass to the function and gives some simple examples of its operation.
Use the ? to generate help on the functions mean, sd, plot and matrix |
Dynamic help
RStudio provides help on the fly. For example, as you are typing a function, the functions that begin with the letters you have typed are displayed as a menu. You can select the one you want, then hit tab to bring it into your statement.
Help is also available by typing a tab after having selected a function. A list of parameters is displayed. Moving among the parameters gives help on each one.| Try this for yourself with some of the functions you have used so far |
Help menu
More extensive help can be obtained from the help files using the [Help] tab of RStudio. Here you can use the Search Engine and Keywords link to access a wide range of information on the operations of R.
Some packages in R have what are called vignettes. These are how-to guides for topics and usually offer gentle introductions and examples. Alternatively, you can view vignettes from any loaded package by going to the ‘Vignettes’ menu and selecting the required package name. This will give a list of all available vignettes for you to open. Sometimes this menu doesn’t appear until you load a package that has a vignette.
The Web
The web and Google are good places to turn for assistance. An excellent quick reference to R can be found on http://www.statmethods.net. Of course, you can also use an AI package of your choice to assist you in navigating RStudio and R. These aids are becoming more and more sophisticated, almost to the point where natural languages, such as English are the new programming language.
Managing Your Workspace
Starting a New Session
RStudio facilitates the management of workflow by defining a workspace to hold your objects – vectors, dataframes, user-defined functions and the like. A workspace and associated files can be saved at the end of a session and reloaded at a later time when you want to continue the analysis.
Managing workflows in RStudio and R can be difficult so we need some basic rules to minimise confusion.
- Identify discrete projects or analyses and create a separate working directory for each one. This way you will avoid having a jumble of objects from many analyses in your workspace.
- Create an RStudio project to facilitate paging in and out of different projects and analyses. Link your RStudio project to your working directory for each defined project.
- Tidy up after each session by removing all unwanted and temporary objects before saving your workspace.
- Use standard file naming conventions, such as
filename.Rfor R programs,filename.csvfor raw comma-delimited data files andfilename.Rdatafor R binary files. - Once you have started RStudio, you need to create a new project using
File > New Project. We will do this later.
RStudio may ask you to save existing work before opening a new project. You should do this if you have important work that has been executed in a previously open project.
Terminating a Session
Exit a session by exiting from RStudio, at which time you will be asked whether you wish to save your workspace.
Resuming a Session
If you have saved your session on exit, RStudio will resume where you left off by reopening the session.
If you have defined projects, you can reopen RStudio and load the project to resume analysis.
Managing your Objects
The active objects associated with your workspace are listed when you select the [Global Environment] tab in RStudio.
In addition, a number of useful functions are useful for managing your workspace.
|
|
Sets the default directory for files, and an action that can also be done from the RStudio menu Session > Working Directory > Choose Directory. |
getwd() |
Gets the default directory. |
ls() |
provides a list of objects in your current workspace, or use the Environment tab. |
rm(object) |
deletes an object from your workspace. |
rm(list=ls()) |
deletes all objects from your workspace. |
sessionInfo() |
provides information about your session. |
Setting a default directory
The best way to manage your work is to keep the files associated with each project in a separate directory on disk. To set the default directory use
setwd("C:/Users/username/Documents/eBook1")
R will then look in the directory eBook1 when locating a file to read, and to write a file. Note the direction of the slashes in the file specification.
Set the working directory to a useful location by adding the above line to your script in the RStudio Editor. You can replace eBook1 with a directory of your choice. Submit the code for execution. |
You can identify the location of the default directory, if you forget where it is, using
getwd()
You can get a listing of your current files in the working directory using
dir()
Alternatively, use the Files tab to view files in your working directory.
Setting up a Project
A project in RStudio is a convenient way to bundle together all the files related to a particular analysis, including code, data, documentation and output. Let’s look at RStudio projects in more detail.
RStudio projects make it straightforward to pick up one of many analyses from where you left off, reproduce your work and collaborate with others. Here’s how they help:
- Isolation: RStudio projects help to ensure that the files associated with an analysis or project are isolated in their own directory, making it easier to organise files, figure out what files are involved in a given analysis, and to move the analysis or project from one computer to another.
- Paths: When you open a project, RStudio sets the working directory to the project’s directory.
- Workspaces: A project has its own R workspace. This allows you to move from one project to another and easily pick up exactly where you left off in each case.
- Version Control Integration: RStudio projects can be integrated with version control systems like Git. Each project can have its own links to a specific repository.
You can create a new project in RStudio by going to File > New Project and then following the prompts to create a new directory or associate an existing directory with a project. This will create an .Rproj file in the directory which stores project-specific settings. By double-clicking on this .Rproj file or re-opening the project, you can reinstate all its associated settings.
Accessing Worked Examples and Exercises
First, set up a project and associated working directory as outlined above. Use a sensible name that you will remember, like eBook1. You only need to do this once.
Data files for the Worked Examples, Exercises and Integrative Exercises covered by this eBook can be downloaded to your working directory by running the following command in your RStudio Console.
source("http://georges.biomatix.org/storage/app/media/eBook1/eBook_data_download.txt")
A list of downloaded files will be shown. They will be in your working directory.
Next, install dartRverse.
install.packages("dartRverse")
Load the package and its libraries into your R Session.
library(dartRverse)
Set the global verbosity to 3.
gl.set.verbosity(3)
Now install the tutorial
install.packages(c("remotes", "learnr"))
remotes::install_github("biomatix/eBook1")
library(eBook1)
To access the worked examples, navigate to the RStudio Tutorial Pane and the session eBook (Figure 1-5).
![]()
Similarly, to access the exercises, navigate to the RStudio Tutorial Pane then open the session eBook and navigate to the exercises for Chapter 1.
Winding Up
Where have we come?
![]()
The above sections were designed to give you an overview of the operation of R through the RStudio graphical user interface. Having completed this Session, you should now be familiar with the following concepts.
- RStudio is an integrated development environment (IDE) with a graphical user interface (GUI) that provides the R console, script editor, graphics, and help tools in one place.
- R packages are first installed, then loaded to become available for use.
- RStudio establishes a workspace. Managing the objects in that workspace can be challenging for new users, but proficiency comes with practice.
- Projects can be established to keep all the data, code and environment in place on save, which allows you to pick up later where you left off.
- R has abundant sources of help, including placing a
?before a command, using tab to list options for a function, referring to the vignette if one has been provided in the loaded packages, and of course, Dr Google.
Ende
Posit, RStudio, and Shiny are trademarks of Posit Software, PBC, all rights reserved, and may be registered in the United States Patent and Trademark Office and in other countries.


![Screenshot of information available using the [Help] tab.](https://canberra.pressbooks.pub/app/uploads/sites/9/2026/07/Figure-1.4-1024x770.jpg)
![Screenshot of Worked examples and exercises available via the [Tutorial] tab](https://canberra.pressbooks.pub/app/uploads/sites/9/2026/07/FIgure-1.5-scaled.png)