# SOCI8015 Lab 1: Introducing R and RStudio

R is a free, open-source programming language for statistical analysis and graphics. RStudio is a free, open-source IDE(integrated development environment) for R and provides a convenient interface of R programming by incorporating many features such as data viewer, full-featured text editor, workspace browser, graphical tools and R help.

# Installing R and RStudio

Both R and RStudio are available for Windows, MacOS, and Linux. But make sure that you must install R before installing RStudio.

## Installing R

1. Go to https://cloud.r-project.org.

### For Windows

1. After clicking on “Download R for Windows”, choose base among Subdirectories.

### For Mac OS

1. After clicking on “Download R for (Mac) OS X”, if you are using Intel Mac, click on “R-4.1.2.pkg”(the latest version in March 2021). If you are using Apple sillicon Mac (M1 Chip), click on R-4.1.2-arm64.pkg.
2. Run the downloaded file, which will guide you to install R. Leave all default settings in the installation option.
3. Also, go to https://www.xquartz.org. Download “XQuartz-2.8.1.dmg”(the latest version in February 2021). Install this dmg file. If you do not install it, you will not be able to use some packages (e.g., summarytools).

## Installing RStudio

2. Scroll down and find a section of “All Installers” (See Figure 1).
3. Choose an installer that supports your OS. For Window, click on RStudio-2022.02.0-443.exe. For MacOS, click on “RStudio-2022.02.0-443.dmg.

# Customising RStudio

Once the installation is completed, the next step is to start RStudio. When you open RStudio the first time, you will see a window consisting of three panes (See Figure 2).

However, this default look of RStudio does not include an important pane where you spend most time during your statistical projects. It is the pane in which you write and save your R codes. To add this pane, click on the first icon with a green plus sign, and then choose “R Script” (See Figure 3).

Then, you will see a new pane in the RStudio (See Figure 4).

Next, I would like to introduce RStudio customisation tips which could make your R programming much easier. To customise RStudio (See Figure 5),

• For Windows, go to Tools in the top menu, and then click on “Global Options…”.
• For Mac OS, go to RStudio in the top menu, and then click on “Preferences…”.

## Setting your default working directory

A default working directory is a folder in which R saves all the files unless you specify another file location. Also, R always opens files from the default working directory. For this reason, it would be convenient to save your files in your default working directory. Otherwise, you may have to type a long file location every time you try to open them.

You can set or change the default working directory by the following steps. In the section of General (of Options window), click on “Browse…” under “Default working directory (when not in a project):” (See Figure 6). It will open a file navigation window.

### For Windows

1. Click New Folder (See Figure 7).
2. Type “r-working”, then hit Enter. It will set the name of new folder as “r-working”.
3. Doubl-click the “r-working” folder that you just made.
4. Click **Open” at the bottom.

### For Mac OS

1. Make sure that your home directory should be highlighted (See Figure 8).
2. Click New Folder at the bottom.
3. Type “r-working” as the name of new folder.
4. Click Create.
5. Make sure that your home directory and “r-working” folder are highlighted (See Figure 9).
6. Click Open at the bottom.

If you follow the steps correctly, you will see “~/r-working” is set as your default working directory.

Of course, you can choose any other folders as your working directory.

Nonetheless, you need to save your R-related files in this working directory. Thus, remember the location of this working folder.

In the section of General, find “Restore previously open source documents at startup” and tick the box of it (See Figure 10). This allows RStudio to open automatically R files on which you have worked. Thus, you can always start with your currently working R files.

Tip: Every time you make changes in RStudio options, do not forget to click on Apply on the bottom. This will save all the changes you made.

## Setting automatic line wrap

Sometimes you may write a very long line of codes, which requires you to move a horizontal scroll bar to read them all. This is annoying. Setting automatic line wrap would be a good choice to avoid this inconvenience.

In the section of Code (in the left) and the tab of Editing (in the top), find “Soft-wrap R source files” and tick the box of it (See Figure 11).

## Customising the look of RStudio

You can change font family, font size, and the look of RStudio in the section of Appearance (See Figure 12).

• RStudio theme: RStudio provides support for two global themes that customize RStudio’s user interface: Modern and Sky. By default, the Modern theme is selected.
• Editor font: Choose a font for panes which display code (Console, Source, History, and Workspace). I recommend Consolas for Windows and Menlo for Mac.
• Font size: Set the font size (in points) for panes which display code (Console, Source, History, and Workspace)
• Editor theme: Specify the visual theme for the Console and Source panes. You can preview the theme using the inline preview or by pressing the Apply button.

## Configuring panes

You can change the layout of four RStudio panes in the section of “Pane Layout”. Figure 13 shows how I configured the four panes.

• Console is a command window. Here you can type any valid R command after the > prompt followed by hitting Enter and R will execute that command.
• Source is a text editor. It is where you edit and save your R codes.
• Bottom left pane includes Environment, Files, Plots, Packages, Help and Viewer. Here you can check datasets, open files, view graphical outcomes, install and load packages, access R help documents, and view markdown and html documents.
• Bottom right pane includes History, Connections, Build and VCS. History shows all R commands that were executed in the Console.

Click on OK at the bottom. Then, you will see newly configured panes (See Figure 14).

# Lab 1 Participation Activity

1. Did you successfully install R and RStudio? Do you see any warning (error) message in R console when you start RStudio? If so, report it to the unit convenor.

1. Did you set the working directory? Confirm that the location is “~/r-working”.

1. Did you customise RStudio as instructed?

Note: Please complete the Lab 1 Participation Activity. You can find the link to this activity on iLearn. This activity will contribute to your participation marks.

Last updated on 03 March, 2022 by Dr Hang Young Lee(hangyoung.lee@mq.edu.au)