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
  2. You will see three links for downloading R which correspond to each OS(operating system). Click on the link that supports your OS.

For Windows

  1. After clicking on “Download R for Windows”, choose base among Subdirectories.
  2. Then, click on “Download R 4.3.2 for Windows”(the latest version in February 2024).
  3. It will start downloading “R-4.3.2-win.exe”. After downloading it, run this file, which will guide you to install R. Leave all default settings in the installation option.

For Mac OS

  1. After clicking on “Download R for (Mac) OS X”, If you are using Apple sillicon Mac (M1 and higher), click on R-4.3.2-arm64.pkg (the latest version in February 2024). if you are using Intel Mac, click on “R-4.3.2-x86_64.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 Download “XQuartz-2.8.5.pkg”(the latest version in February 2024). Install this pkg file. If you do not install it, you will not be able to use some packages (e.g., summarytools).

Installing RStudio

  1. Go to
  2. Scroll down and find a section of “All Installers and Tarballs” (See Figure 1).
  3. Choose an installer that supports your OS. For Window, click on RSTUDIO-2023.12.1-402.EXE. For MacOS, click on “RSTUDIO-2023.12.1-402.DMG.
  4. Install the downloaded file. Choose default installation options.
Download RStudio

Figure 1: Download RStudio

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

First Look of RStudio

Figure 2: First Look of RStudio

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

Adding R Text Editor

Figure 3: Adding R Text Editor

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

New RStudio Look

Figure 4: New RStudio Look

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

Go to Tools in the top menu, and then click on “Global Options…”. (This is the same for PC and Mac)

New RStudio Look

Figure 5: New RStudio Look

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.

Default Working Directory

Figure 6: Default Working Directory

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.
Making a Working Directory for Windows (1)

Figure 7: Making a Working Directory for Windows (1)

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.
Making a Working Directory for Mac (1)

Figure 8: Making a Working Directory for Mac (1)

Making a Working Directory for Mac (2)

Figure 9: Making a Working Directory for Mac (2)

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.

Automatically loading your previous R codes

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.

Working with Your Previous R Codes

Figure 10: Working with Your Previous R Codes

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). Then, click the tab of Saving (in the top) and tick “Always save R scripts before sourcing” (See Figure 12). Then, click Apply at the bottom.

Soft Line Wrap

Figure 11: Soft Line Wrap

Automatic Saving of R Scripts

Figure 12: Automatic Saving of R Scripts

Customising the look of RStudio

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

  • 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.
Customising Appearance

Figure 13: Customising Appearance

Configuring panes

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

Configuring Panes

Figure 14: Configuring 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 15).

Configuring Panes

Figure 15: Configuring Panes

Lab 1 Participation Activity

  1. Did you successfully install R and RStudio? For Mac users, make sure that you must install “XQuartz-2.8.5.pkg” as well.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?

If you say “No” to any of the three questions, please contact the unit convenor by email. He will help you to fix the problem.

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 21 February, 2024 by Dr Hang Young Lee(