3  Setting Up the R Environment and IDEs

NoteWhat This Chapter Covers

This chapter walks you through getting R running on your own machine and choosing an Integrated Development Environment (IDE) to write code in. You will learn how to install R from the Comprehensive R Archive Network (CRAN), how to install RStudio as your primary IDE, how to perform a first-run health check to confirm your setup is working, and how to decide between RStudio, Visual Studio Code, Jupyter, and other alternatives for different kinds of work. By the end of this chapter your machine will be ready for every subsequent chapter in the book.

flowchart LR
    R["R Language <br> (Download from CRAN)"] --> IDE["An IDE <br> (RStudio / VS Code / Jupyter)"]
    IDE --> PROJ["An R Project <br> (Organised workspace)"]
    PROJ --> WORK["You write code <br> and analyse data"]
    style R fill:#e3f2fd,stroke:#1976D2
    style IDE fill:#fff3e0,stroke:#F57C00
    style PROJ fill:#f3e5f5,stroke:#8E24AA
    style WORK fill:#e8f5e9,stroke:#388E3C


3.1 The Two Things You Need

NoteCore Concept: R and an IDE Are Separate

A common source of confusion for new users is treating RStudio and R as the same thing. They are not. R is the language and the engine that executes your code. An IDE such as RStudio is a separate application that provides a friendly place to write and run that code. You can technically use R without any IDE by typing commands into the terminal, and you can swap IDEs without reinstalling R. For this book we will install both.

Component What It Is Where to Get It
R The language interpreter, base libraries, and command-line console. cran.r-project.org, the Comprehensive R Archive Network.
RStudio An IDE built specifically for R, now published by Posit. posit.co/download/rstudio-desktop.
Packages Optional extensions to R, installed from within R itself using install.packages(). CRAN, Bioconductor, or GitHub.
TipExpert Insight: Install in the Right Order

Always install R first, then RStudio. RStudio looks for an existing R installation when it starts, and skipping this order is the most common cause of a “cannot find R” error on a fresh machine.


3.2 Installing R from CRAN

NoteHow To: Install R on Windows, macOS, or Linux
  1. Open a browser and go to https://cran.r-project.org.
  2. On the front page, click the download link for your operating system: Download R for Windows, Download R for macOS, or Download R for Linux.
  3. On Windows, click base, then the large link that begins with Download R x.y.z for Windows. Run the downloaded .exe and accept the defaults.
  4. On macOS, click the .pkg file matching your hardware. Apple Silicon Macs need the arm64 build; older Intel Macs need the x86_64 build. Open the downloaded .pkg and follow the installer.
  5. On Ubuntu or Debian, follow the CRAN instructions to add the CRAN repository and then run sudo apt install --no-install-recommends r-base. On Fedora or RHEL, use sudo dnf install R.
  6. When installation finishes, open your operating system’s application launcher and confirm that an application called R is now available.

[Insert screenshot of the CRAN download page showing the three operating system links.] [Insert screenshot of the Windows R installer wizard at the component-selection step.]

TipBest Practice: Always Install the Latest Stable Release

R has a new major release each spring and minor releases a few times a year. The latest stable release contains performance improvements and fixes that packages increasingly depend on. Unless your organisation mandates a specific R version for a regulatory reason, install the newest stable release.

Warning32-bit R Is Deprecated

Older Windows installers offered both 32-bit and 64-bit R. From R 4.2 onward, the Windows installer ships only the 64-bit version. If you are following instructions from an older tutorial that asks you to pick between 32-bit and 64-bit, ignore that step and proceed with the default.


3.3 Verifying Your Installation

NoteHow To: Confirm R Is Installed Correctly

Open the R application that the installer placed on your machine. You should see a window titled R Console with a greeting similar to the one below. Type the following at the > prompt and press Enter.

TipWhat to Do If Something Fails

If the R window does not open at all, reinstall R and confirm that your user account has permission to install applications. If the window opens but 2 + 2 does not return 4, your installation is corrupt and reinstalling is the fastest fix. Errors about locale settings on Linux are rarely fatal and can usually be ignored for the purposes of this book.


3.4 What Is an IDE?

NoteCore Concept: Why an IDE Matters for R

An IDE, or Integrated Development Environment, bundles together the tools you need to write, run, and debug code in one window. For R specifically, an IDE typically gives you a script editor with syntax highlighting, a console to run code interactively, a workspace viewer that shows your variables and data frames, a plot viewer, a file browser, and a help pane. Using R without an IDE is possible, but you would constantly be switching between terminal windows, an external text editor, and a separate plotting viewer. An IDE collapses that friction to near zero.

flowchart TD
    IDE["An R IDE"] --> E["Script Editor <br> (write code)"]
    IDE --> C["Console <br> (run code interactively)"]
    IDE --> W["Workspace / Environment <br> (see your variables)"]
    IDE --> P["Plot Viewer <br> (see charts)"]
    IDE --> H["Help + Files <br> (docs, packages, projects)"]
    style IDE fill:#e3f2fd,stroke:#1976D2
    style E fill:#fff3e0,stroke:#F57C00
    style C fill:#fff3e0,stroke:#F57C00
    style W fill:#fff3e0,stroke:#F57C00
    style P fill:#fff3e0,stroke:#F57C00
    style H fill:#fff3e0,stroke:#F57C00


3.5 RStudio Desktop: The Recommended IDE

NoteHow To: Install RStudio Desktop
  1. Go to https://posit.co/download/rstudio-desktop.
  2. Scroll to the download table and pick the installer that matches your operating system.
  3. On Windows, run the .exe and accept defaults. On macOS, open the .dmg and drag the RStudio icon to the Applications folder. On Linux, download the .deb or .rpm and install with your package manager.
  4. Launch RStudio from your application launcher. It will automatically detect the R installation you set up in the previous section.

[Insert screenshot of the Posit download page highlighting the free RStudio Desktop download.] [Insert screenshot of a fresh RStudio window showing the four default panes.]

NoteThe Four Panes of RStudio
Pane Default Position Purpose
Source editor Top left Where you write, save, and edit R scripts and Quarto documents.
Console Bottom left Where code is executed; also usable as an interactive REPL.
Environment and History Top right Shows the variables, data frames, and functions in your current session.
Files, Plots, Packages, Help, Viewer Bottom right File browser, plot output, package manager, help viewer, and Viewer for Quarto or Shiny previews.
NoteEssential First-Time Settings

Before you start writing code in RStudio, change two default settings that matter a great deal for reproducible work.

  1. Open ToolsGlobal Options.
  2. On the General tab, uncheck Restore .RData into workspace at startup.
  3. On the same tab, set Save workspace to .RData on exit to Never.
  4. Click Apply.

These two changes ensure that every R session starts from a clean state, which is essential for reproducibility. Without them, invisible leftovers from earlier sessions can silently change the behaviour of your scripts.

[Insert screenshot of the RStudio Global Options dialog showing the General tab with the two workspace settings unchecked.]

TipExpert Insight: Use RStudio Projects From Day One

An RStudio Project is a folder on disk plus a small .Rproj file that RStudio uses as a context. Opening a project automatically sets your working directory, isolates your workspace, and keeps your file browser focused on the project folder. Working outside projects leads to fragile code that depends on whichever folder you happened to launch R from. Create a new project for every book chapter, assignment, or analysis: FileNew ProjectNew Directory.

WarningCommon Mistake: Editing in the Console Instead of a Script

New users often type every command directly into the console. This works but is not reproducible. Write code in a script file in the source editor, save it, and run lines with Ctrl+Enter on Windows or Cmd+Enter on macOS. Your script is then a durable record of what you did.


3.6 Alternative IDEs

NoteWhen Another IDE Might Fit Better

RStudio is the default recommendation for this book. The alternatives below are worth knowing about because you may encounter them in workplaces, research groups, or teaching contexts that have standardised on a different tool.

IDE Best For Notes
Visual Studio Code with the R extension Polyglot developers who already live in VS Code for other languages. Install the R extension by Yuki Ueda, plus the languageserver R package. Strong for multi-language projects.
Jupyter with the IRkernel Teams doing data science across Python and R in shared notebooks. Install the IRkernel R package and register it with Jupyter. Great for exploratory notebooks; weaker for long projects.
Emacs with ESS (Emacs Speaks Statistics) Experienced Emacs users who prefer keyboard-centric workflows. Steep learning curve, powerful once mastered. Common in academic research groups.
Positron Early adopters curious about Posit’s next-generation IDE. A newer Posit IDE building on VS Code foundations, still evolving, supports both R and Python first-class.
R GUI (base) Quick, one-off tasks with no IDE setup. The plain R application that ships with the R installer. Minimal but always available.
TipExpert Insight: The IDE Is Not the Language

Switching IDEs does not change what R can do. Every example in this book will run unchanged in any of the IDEs above, because they all ultimately send code to the same R interpreter. Pick the IDE that fits your workflow and stay curious about alternatives rather than anxious about them.


3.7 A First-Run Health Check

NoteHow To: Confirm Your Environment Is Ready for the Rest of the Book

Run the short sequence below. It exercises the parts of R you will use most in Module 1: basic arithmetic, vector creation, summary statistics, and a quick plot. If every line runs without error, your environment is ready.

TipA Minimum Package Set for This Book

The chapters ahead introduce packages gradually. For a smooth experience, plan to install these core packages in your local R installation before Module 3:

Package Role in This Book
dplyr Grammar of data manipulation; used throughout Modules 2 and 3.
tidyr Reshaping and tidying data frames.
ggplot2 Publication-quality visualisation.
readr Reading CSV and delimited files.
stringr Convenient string manipulation.
WarningDo Not Run install.packages() Inside This Book’s Interactive Chunks

The interactive chunks in this book run in WebR, which pre-loads the packages used by our examples. Calling install.packages() from inside a WebR chunk is unnecessary and will produce confusing download output. Install packages in your local R installation instead, using either the Packages pane in RStudio or the standard install.packages("name") command at the console.


3.8 Summary

NoteKey Concepts at a Glance
Concept Key Takeaway
R and the IDE are separate R is the language and engine; RStudio, VS Code, and Jupyter are IDEs that talk to R.
Install R first Download and install R from CRAN before installing any IDE.
RStudio is the recommended IDE Familiar layout, strong R-specific features, and first-class support for Quarto and R Markdown.
First-time settings Turn off Restore .RData and set Save workspace to Never for reproducible sessions.
Projects from day one Use RStudio Projects to keep each analysis self-contained.
Alternatives exist VS Code, Jupyter, Emacs ESS, and Positron are viable choices depending on your context.
Health check A short arithmetic, vector, and plot sequence is all you need to confirm readiness.
TipApplying This in Practice

A well-set-up environment is the foundation everything else rests on. Invest fifteen minutes now in installing R, installing RStudio, changing the two workspace settings, and running the health check. That upfront investment saves hours of debugging later. In the next chapter you will start writing actual R code, beginning with the language’s basic syntax, the three ways to assign variables, and the naming conventions that keep your scripts readable to future-you and to colleagues.