0% found this document useful (0 votes)
13 views14 pages

R Basics RStudio

materials about R Basics RStudio

Uploaded by

yc47398
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views14 pages

R Basics RStudio

materials about R Basics RStudio

Uploaded by

yc47398
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

RStudio: Quick Start

Shu Fai Cheung @ University of Macau

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.

Figure 1: The default RStudio layout

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.

SSGC8802 (Aug 2024) R Language


3

Figure 2: Choose New R Script from File

2.4 Environment
By default, on the top right is the Environment pane (Figure 3). This pane shows all objects in the
environment.

Figure 3: A sample workspace pane

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.

Figure 4: A sample history pane

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.

SSGC8802 (Aug 2024) R Language


4

Figure 5: The file pane

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.

Figure 6: A sample plot pane

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.

SSGC8802 (Aug 2024) R Language


5

Figure 7: The packages pane

2.9 Help
The help pane (Figure 8) is used to display help pages.

Figure 8: A sample help page

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,

2.10 Customize the Environment (Panes)


You can also customize the layout of the panes. Chose Tools -> Global Options -> Pane Layout and
arrange them to suit your style (Figure 10).

3 How-To’s
The following sections are organized by tasks. Some of them repeat information presented above.

SSGC8802 (Aug 2024) R Language


6

Figure 9: Help through the search file

Figure 10: The dialog box to customize pane layout

SSGC8802 (Aug 2024) R Language


7

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).

Figure 11: A sample R script in the source pane

The RStudio source pane has automatic syntax highlighting. This is very useful for reading a script.

3.1.2 How to run one or more lines of code?


In RStudio, you can use Control-Enter to run selected lines.
You can also use the Run button on the top of the pane.

3.1.3 How to open and save script?


While in the source pane, you can use the Open File …, Save, and Save as … from the File pull down
menu (Figure 12).
You can also use the open and save icons at the top of the pane.
Conventionally, “.R” is used as the file extension for R scripts.

3.1.4 How to get a list of potential functions while typing?


If you forget the full name of a function, or just want to save time when typing, you can type the first few
characters of a function, and then press Tab (Figure 13). A list of potential functions will be shown.
You can also highlight a function and then click F1. The help page for the function will be shown in the
help pane.

3.1.5 How to get help for a function?


You can get the help page of a function simply by placing the cursor on the function, and click F1.
For example, in the following screenshot, the cursor is on mean. If you click F1, the help page for mean
will be opened (Figure 14).

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.

SSGC8802 (Aug 2024) R Language


8

Figure 12: The file pull down menu

Figure 13: An example of function list by tab

SSGC8802 (Aug 2024) R Language


9

Figure 14: An example of help by F1

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.

Figure 15: Compile Report Button

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.2.2 How to View a Data Frame?


For each dataset (usually data frame), you can find a view button on the right (Figure 18).
If you click this button, the content of the dataset will be displayed in a tab in the source pane (Figure 19).

SSGC8802 (Aug 2024) R Language


10

Figure 16: Compile Report Format

Figure 17: A sample workspace pane

Figure 18: The view data button

SSGC8802 (Aug 2024) R Language


11

Figure 19: The content of the dataset displayed in a source tab

3.2.3 How to save the current workspace?


NOTE: This section has been deleted. It is more reliable to use scripts or Quarto file, with data import
and export functions, to ensure reproducibility. I do not recommend saving the workspace. But do so if
this fits your working style.

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.

Figure 20: The packages pane

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.

3.3.2 How to install a package?


You can use the Install packages button in the packages pane (Figure 21).
This opens the install packages dialog (Figure 22).

SSGC8802 (Aug 2024) R Language


12

Figure 21: The install button

Figure 22: The install packages dialog

This dialog box also has autocompletion. You can type a few characters and then select the package
from the list.

3.3.3 How to Update Packages?


You can use the Check for Updates button in the packages pane to check which packages have updates.
You can then choose which packages to update (Figure 23).

Figure 23: The check for updates button

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.4.1 How to Zoom in a Plot?


In the plot pane, if you click the zoom button, the current plot will be opened in a new window (Figure 24).
You can then resize the plot as you see fit (Figure 25).

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.

SSGC8802 (Aug 2024) R Language


13

Figure 24: The zoom button in the plot pane

Figure 25: The new plot window

SSGC8802 (Aug 2024) R Language


14

3.5.1 How to read a data file in text format (e.g., CSV)?


You can use the Import dataset button in the workspace/environment pane to import a dataset. For
example, you choose From Text File … (Figure 26).

Figure 26: The import dataset button

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.

SSGC8802 (Aug 2024) R Language

You might also like