Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 39 ยท Getting Started

Install R and RStudio

Install R and the RStudio IDE on Windows, macOS or Linux, tour the four panes, and run your first commands in the console and in a script file.

To follow this course you need two programs. R is the language itself: the engine that runs your code. RStudio (from Posit) is an editor built around R. It gives you a script editor, a console, a plot viewer and a file browser in one window. You can use R without RStudio, but almost everyone starts with it because it makes the work much more comfortable.

This lesson walks through installing both, understanding the window you will be staring at, and running your first commands.

Step 1: Install R

R is free and open source. Download the installer for your system from CRAN, the Comprehensive R Archive Network (cran.r-project.org):

  • Windows: choose "Download R for Windows", then "base", and run the installer with the default options.
  • macOS: choose "Download R for macOS" and pick the build that matches your Mac (Apple silicon or Intel).
  • Linux: use your package manager (for example sudo apt install r-base on Debian or Ubuntu), or add the CRAN repository for a newer version.

Install R first, then RStudio. RStudio only finds R if R is already there.

Step 2: Install RStudio

Download the free RStudio Desktop installer from posit.co and run it. Then open RStudio. If it starts without errors, both pieces are working.

No installation? Posit Cloud (a browser-based RStudio) lets you practice without installing anything. It is handy on a locked-down computer, though the free plan has usage limits.

A tour of RStudio

When you open RStudio and create a new script (File, New File, R Script), the window shows four panes:

The four RStudio panes

Source Top left: scripts you save
Console Bottom left: where R runs code
Environment Top right: your variables
Files, Plots, Help Bottom right: output and docs

Pane positions can change in Preferences.

1 / 2
  • Source pane is a text editor for .R script files. Your real work lives here.
  • Console is where R actually executes commands. You see the prompt >. Type a line and press Enter.
  • Environment lists the variables you have created, with their sizes.
  • Files / Plots / Packages / Help shows your folders, charts, installed packages and documentation pages.

Your first commands in the console

Click in the console, type each line, and press Enter:

R
1 + 1
#> [1] 2

10 / 4
#> [1] 2.5

sqrt(81)
#> [1] 9

"Hello, R!"
#> [1] "Hello, R!"

print("Hello again")
#> [1] "Hello again"

Two things to notice. First, R prints the result of every expression automatically. Second, text values (strings) are written in quotes. Single or double quotes both work, but double quotes are the usual style.

If the console shows a + instead of >, R thinks your command is unfinished, for example because you forgot a closing parenthesis. Press Esc to cancel and start the line again.

Running code from a script

The console forgets your work when you close it. A script saves your commands in a file so you can rerun them. Type this into a new script:

R
# my-first-script.R
name <- "Amina"
hours <- c(2, 3.5, 1, 4)

total <- sum(hours)
cat("Hello,", name, "\n")
cat("You studied", total, "hours this week\n")

To run it:

  • Ctrl+Enter (Cmd+Enter on a Mac) runs the current line, or the selected lines.
  • Ctrl+Shift+Enter runs the whole script (the "Source" button).

Running the script should print:

Text
Hello, Amina 
You studied 10.5 hours this week

cat() prints plain text without the [1] marker and without quotes. Note the \n, which means "start a new line". cat() does not add one by itself. (cat also puts a space between its arguments, which is why "Amina" is followed by a space before the line break.)

Save the file with Ctrl+S using a name without spaces, for example first-script.R.

Working directory and projects

R always has a working directory: the folder it reads files from and saves files to by default. Check it with:

R
getwd()

Beginners often get "file not found" errors because the working directory is not where they think. The cleanest fix is an RStudio Project. Choose File, New Project, and pick a folder. RStudio then opens with that folder as the working directory every time, and your scripts, data and results stay together. Use one project per analysis from day one.

Avoid setwd("C:/Users/you/...") at the top of scripts. That path only exists on your computer, so the script breaks for anyone else. Use projects and relative paths such as "data/sales.csv" instead.

Restart habits that save trouble

RStudio can save your workspace when you quit and reload it next time. That sounds convenient but hides bugs: your script may work only because of a variable left over from earlier. In Tools, Global Options, General, set "Save workspace to .RData on exit" to Never and untick restoring on startup. Then use Session, Restart R now and then, and run your script from the top. If it still works, it is reproducible.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the relationship between R and RStudio?

    R runs the code. RStudio is an interface that makes writing and running R code easier. Install R first.
  2. 2. The console shows + instead of >. What does that usually mean?

    + is the continuation prompt. Press Esc to cancel and retype the command.
  3. 3. Which function prints text without the [1] marker or quotes?

    cat() writes plain text. You must add \n yourself if you want a line break.
  4. 4. Why prefer RStudio Projects over setwd() with an absolute path?

    A project sets the working directory to its own folder, so relative paths work on any computer.

Summary

  • Install R from CRAN, then RStudio Desktop from Posit.
  • RStudio has a source editor, console, environment and files/plots/help panes.
  • Try ideas in the console; save work in .R scripts and run it with Ctrl+Enter.
  • Use an RStudio Project so relative file paths work, and avoid saving the workspace.

Next: R Basics: Values, Variables and Operators