R and RStudio

Author

Mark Andrews

Abstract

This guide covers the first steps in setting up a working R environment. It explains the relationship between R and RStudio, walks through installation, introduces the four-pane layout, explains how to install and load packages, covers the global settings most worth changing, and shows how to create and use RStudio Projects to keep your work organised and reproducible.

R and RStudio

R and RStudio are two separate pieces of software that work together. R is the statistical computing engine: it does all the calculation. RStudio is an integrated development environment (IDE) that provides a comfortable interface for working with R — a code editor, a console, a file browser, a plot viewer, and more.

Think of it like a car. R is the engine. RStudio is the dashboard, the steering wheel, and the controls. You can technically run R without RStudio, but most people do not; RStudio makes working with R far easier. You cannot use RStudio without R, because the interface needs an engine to run.

The standard workflow is: install R, then install RStudio, then open RStudio and never need to open R directly again.

Installing R

Go to https://cran.r-project.org and download the installer for your platform.

On Windows: click Download R for Windows, then base or install R for the first time, download the .exe, and run the installer with the default settings.

On macOS: click Download R for macOS, choose the installer matching your hardware (Apple Silicon or Intel), download the .pkg, and run it.

Once installed, you can verify by opening the plain R application and checking that it launches. Close it again before installing RStudio.

Installing RStudio

Go to https://posit.co/download/rstudio-desktop and download the RStudio Desktop installer for your platform. Run the installer and accept the defaults.

Once both are installed, open RStudio — not the plain R application. You will use RStudio for everything from here on.

A tour of the RStudio interface

RStudio organises its interface into four panes. Their positions can be rearranged, but the defaults are sensible and assumed throughout this guide.

Console pane (bottom-left). This is the direct interface to R. Type a command after the > prompt and press Enter to run it immediately. Results, warnings, and error messages appear here.

Editor pane (top-left). Your main workspace for writing and saving code. R scripts (.R) and Quarto documents (.qmd) open here as tabs. The editor provides syntax highlighting, code completion, and the ability to send code to the console with Ctrl+Enter (Windows) or Cmd+Enter (macOS).

Environment pane (top-right). Shows every object currently in your R session — data frames, vectors, model results, and so on. The History tab records every command you have run.

Files / Plots / Packages / Help pane (bottom-right). A multi-purpose panel. Files is a file browser for your project. Plots shows graphics you create. Packages lists installed packages. Help displays documentation — typing ?function_name in the console opens the relevant page here.

Key menus

The menus across the top give access to almost everything in RStudio. The ones most relevant at the start are:

File. Create new scripts and projects, open existing files.

Code. Run lines or the whole script. Restart R gives you a clean slate.

Tools. Global Options is the main configuration panel — this is where you adjust the settings described below.

Help. Access documentation and cheatsheets.

Installing packages

R ships with a solid core toolkit. Its real power comes from the thousands of add-on packages contributed by the community. Packages are installed once and then loaded into each session as needed.

To install a package, run install.packages() in the console:

install.packages("tidyverse")

You can install several packages at once by passing a character vector:

install.packages(c("tidyverse", "skimr", "emmeans"))

Installation pulls in the package and any packages it depends on, which may be many. This only needs to be done once per machine.

Loading packages

Installing puts the package on your disk. To use it in an R session you must load it with library():

library(tidyverse)
library(skimr)
library(emmeans)

When you load tidyverse you will see a startup message listing the eight packages it attaches and noting any function name conflicts with base R. The conflicts message is routine and does not indicate a problem.

Loading a package is required each time you start a new R session. Always include your library() calls at the top of your scripts so the script is self-contained.

Global settings worth changing

Open Tools > Global Options before you write any code. A few changes here prevent common problems and improve your daily workflow.

The blank-slate rule

By default, R saves all objects in your workspace when you quit and restores them silently next time. This sounds helpful but causes subtle problems: old objects accumulate, your script appears to work on your machine because it depends on objects that were never created by the script itself, and the analysis becomes impossible for anyone else to reproduce.

Turn this off. In Global Options > General, untick Restore .RData into workspace at startup and set Save workspace to .RData on exit to Never.

With this setting, every session starts clean. Your script must create everything it needs. This is the right discipline, and it forces you to keep the script complete and trustworthy.

The native pipe shortcut

Since R 4.1, the base language includes the pipe operator |>, which passes the result of one expression into the next. It appears throughout this course. A keyboard shortcut makes it easy to type.

In Tools > Global Options > Code > Editing, tick Use native pipe operator. The shortcut Ctrl+Shift+M (or Cmd+Shift+M on macOS) then inserts |> with correct spacing.

Visual aids

A few display settings in Code > Display reduce simple errors:

  • Rainbow parentheses colours each level of bracket nesting, making missing brackets obvious.
  • Show line numbers helps when discussing code in class.
  • Soft-wrap R source files avoids horizontal scrolling on narrower screens.

Theme and appearance

In Appearance, choose an editor theme and font size that suits your eyes. Dark themes like Cobalt work well for extended sessions. The zoom level can be adjusted any time with Ctrl+= or Ctrl+- (Cmd on macOS).

RStudio Projects

Whenever you work in RStudio, use an RStudio Project. A Project is a folder that holds all your files — scripts, data, and output — plus a small .Rproj file that tells RStudio that this folder is a project. When you open a Project, RStudio sets R’s working directory to that folder automatically.

This matters because file paths in your scripts become relative to the project root. A script that reads read_csv("weight.csv") will find the file regardless of where the project folder sits on your machine, as long as weight.csv is in the project folder. Without Projects, file paths are typically absolute and break on every other machine.

To create a new project:

  1. Choose File > New Project.
  2. Select New Directory > New Project.
  3. Give the folder a short, descriptive name — for example, isurr-workshop.
  4. Choose a sensible parent location and click Create Project.

RStudio opens the new project. The title bar shows the project name. The working directory is now set to that folder.

To come back to a project later, use File > Open Project or pick it from File > Recent Projects.