R Basics RStudio
R Basics RStudio
Aug 2024
Table of contents
1 Background 1
1.1 Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2 Default Environment 2
2.1 The Default RStudio Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.2 Console . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.3 Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.4 Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.5 History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.6 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.7 Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.8 Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.9 Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.10 Customize the Environment (Panes) . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3 How-To’s 5
3.1 Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.1.1 Where to edit a script? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.1.2 How to run one or more lines of code? . . . . . . . . . . . . . . . . . . . . . . 7
3.1.3 How to open and save script? . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.1.4 How to get a list of potential functions while typing? . . . . . . . . . . . . . . . . 7
3.1.5 How to get help for a function? . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.1.6 How to have One Single File with the Code and the Output? . . . . . . . . . . . 7
3.2 Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.2.1 What Do We Have in Our Environment? . . . . . . . . . . . . . . . . . . . . . 9
3.2.2 How to View a Data Frame? . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.2.3 How to save the current workspace? . . . . . . . . . . . . . . . . . . . . . . . 11
3.3 Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.3.1 How to Load or Unload a Package? . . . . . . . . . . . . . . . . . . . . . . . . 11
3.3.2 How to install a package? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.3.3 How to Update Packages? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.4 Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.4.1 How to Zoom in a Plot? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.5 Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.5.1 How to read a data file in text format (e.g., CSV)? . . . . . . . . . . . . . . . . . 14
4 Final Remarks 14
1 Background
1.1 Scope
This is a “Quick Start” guide to RStudio for those who have never learned RStudio. I have tried to include
some new features. However, RStudio is updated frequently and so there may be new useful features
1
2
not covered here. You can refer to the official website to learn more about RStudio
• Using RStudio.
I want to keep this guide extremely simple, and so I left out many features of RStudio. In some cases,
even if there are more than one way to do a task, I may intentionally and arbitrarily select only one.
R scripts will be used in this handout for illustration, although another format, Quarto, is becoming more
and more popular. We will cover Quarto later.
2 Default Environment
2.1 The Default RStudio Layout
The default layout is shown in Figure 1. Check the startup message in the console window (left by default)
and see whether this is the installation of R you want to use.
2.2 Console
It is the same console window in R default GUI (graphical user interface). If you have learned the default
R GUI before, you can use it as usual. If you study books on the default R GUI, you can also use this
console window in the same way. There are some additional features to be introduced below.
2.3 Source
By default, there is no source pane. Choose File -> New -> R Script (Figure 2). A new source pane
will appear.
The pane above is similar to the editor window in R default GUI. However, it is multi-tabbed. You can
open more than one file in this pane.
For usual R scripts, you can write the script here and use Control-Enter to run highlighted lines.
2.4 Environment
By default, on the top right is the Environment pane (Figure 3). This pane shows all objects in the
environment.
The environment pane gives you an instant overview of what you have in the current environment (the
“workspace”, or the “global environment”).
2.5 History
The history pane (Figure 4) by default is next to the environment pane. This pane stores all previous
function calls. You can select one or more lines and copy and paste them to the console pane by the To
Console button.
2.6 Files
The files pane (Figure 5) is a simple file browser. You can use it to find and open files.
You can also use it to choose and set the working directory. The small … icon on the top right of the
pane open a dialog box to choose a folder/directory.
2.7 Plots
In RStudio, if you generate a graph in the R console, the graph will be placed in the Plots tab (Figure 6).
This plot is not useful for production use. Nevertheless, it can be used for us to have a quick look of what
a plot may appear.
2.8 Packages
The packages pane (Figure 7) gives you an instant list of all packages installed for the chosen installation
of R.
In a way, packages are like “apps” in mobile phones. You need to install them to use them.
Once installed, you still need to load them such that functions inside them become available. Just like
an app, you may need to start an app to use it.
You can easily load or unload (detach) a package using the checkbox on the left of this package.
• For example, in the previous screenshot, base is loaded, but apaTables is not.
You can also use this pane to install or update packages, and check for updates for installed packages.
2.9 Help
The help pane (Figure 8) is used to display help pages.
If you type ?mean in the console, instead of opening the help page in a web browser, the help page will
be displayed in the help pane.
You can also use the search field to find help pages (Figure 8).
For example,
3 How-To’s
The following sections are organized by tasks. Some of them repeat information presented above.
3.1 Script
3.1.1 Where to edit a script?
We can use the source pane to edit script, as in the editor window in R default GUI (Figure 11).
The RStudio source pane has automatic syntax highlighting. This is very useful for reading a script.
3.1.6 How to have One Single File with the Code and the Output?
Quarto will be covered here. We illustrate how to do this with R script.
First, save your script.
Then find and click the Compile Report button (Figure 15). You will then be prompted with the dialog
box on the format (Figure 16). We choose HTML for now. You may then be prompted to install some
packages. Just install them.
The script will then be run, and the results stored in an HTML file.
This simple way is good enough for some scenarios. A more advanced format, Quarto, will be introduced
later.
3.2 Environment
3.2.1 What Do We Have in Our Environment?
You can find what objects you have in the environment pane. This is the same list you get by ls(), with
more information.
For example, in Figure 17, there are three objects: dataset1 (a data frame with five observations/cases
and two variables/columns), name (a vector of five strings), and x (a vector of five numbers).
3.3 Packages
3.3.1 How to Load or Unload a Package?
You can easily load or unload (detach) a package by the checkbox on the left of it.
For example, in Figure 20, graphics is loaded, but boot is not.
To load the boot package, you simply check the box to the left of it.
• This is equivalent to use library(boot) in the console or the script.
This dialog box also has autocompletion. You can type a few characters and then select the package
from the list.
3.4 Plots
NOTE: I do not recommend generating plots this way for reports. It is better to do it inside a Quarto file.
However, if you are certain you just need some quick plots, the steps below are what you can do. I also
use this to check tests some functions quickly.
3.5 Dataset
NOTE: I recommend reading files inside a script or Quarto file. Again, if you need to import something
quick just for inspection, you can follow the steps below.
After you select the text file to import, the Import Dataset dialog box will appear. You can consider this
as a graphical interface to read.table().
4 Final Remarks
There are many online resources on learning RStudio. You can start here: https://education.rstudio.com/
learn/beginner/.
There are other alternatives to RStudio. I myself actually rarely use RStudio. I use the default R GUI
(yes, really) and Visual Studio Code (https://code.visualstudio.com/). Use whatever interface that fits
your working style.