Skip to main content

Getting Started with RStudio

R Basics
Software
Setup
A beginner’s tour of RStudio: the four panes, projects, writing and running scripts, the most useful tabs, settings worth changing on day one, and the keyboard shortcuts that save the most time.
Author

Arun Kumar Mahesh

Published

December 6, 2026

In the RGui post you learned how R itself works: typing code at the prompt, running scripts and installing packages. RStudio does all of that too, but puts everything you need in one window and adds many time-saving features. It’s the editor most R programmers use every day, in pharma and elsewhere.

By the end of this post you’ll be able to:

Note

Haven’t installed RStudio yet? Follow How to Install R and RStudio on Windows and Mac first. RStudio needs R installed to work.

1. Opening RStudio

Open RStudio, not R. On Windows, find it in the Start menu. On a Mac, open it from Applications.

RStudio finds your R installation automatically. The Console shows the same R version message you saw in RGui, which confirms R is running inside RStudio.

2. The four panes

RStudio’s window is split into four areas, called panes:

Left Right
Top Source: your scripts and documents Environment: your data and objects
Bottom Console: where R runs code Files / Plots / Packages / Help / Viewer

Diagram of the RStudio window: Source top left, Environment top right, Console bottom left, and Files, Plots, Packages, Help and Viewer bottom right

The four panes of the RStudio window and what each one is for
Tip

The Source pane only appears once you open or create a file. If you see just three panes, go to File → New File → R Script.

Source (top left)

This is where you write and save code. Each open file gets its own tab. Besides R scripts (.R), you can open Quarto documents (.qmd), CSV files and many other text files here.

Console (bottom left)

This is the same R console you used in RGui. Code you run from a script is sent here, and the results appear here. You can also type quick commands directly at the > prompt.

Next to it are two more tabs:

  • Terminal: a command line for your computer, useful for Git or Quarto commands.
  • Background Jobs: runs long scripts in the background while you keep working.

Environment (top right)

The Environment tab lists every object you’ve created in your session: data frames, vectors, functions and so on. For a data frame it shows the number of rows (obs.) and columns (variables).

  • Click the blue arrow beside a data frame to see its columns.
  • Click its name to open it in a spreadsheet-style viewer.
  • Click the broom icon to remove all objects.

The same pane also has:

  • History: every command you’ve run. Select one and click To Console or To Source to reuse it.
  • Connections: connect to databases.
  • Git: appears when your project uses Git version control.

Files, Plots, Packages, Help and Viewer (bottom right)

Tab What it’s for
Files Browse, open, create, rename and delete files and folders in your project
Plots Shows charts you create. Use the arrows to move between plots and Export to save one as an image or PDF
Packages Lists installed packages. Tick a box to load a package, or click Install or Update
Help Shows help pages for functions
Viewer Shows web content such as interactive tables, HTML reports and Shiny apps

3. Projects: keep each piece of work in its own folder

An RStudio project is a folder that holds everything for one piece of work: scripts, data and outputs. Projects are the single most useful habit to build early.

When you open a project, RStudio:

  • sets the working directory to the project folder, so you never need setwd()
  • reopens the scripts you had open last time
  • keeps each project’s history separate

Create a project

  1. Go to File → New Project…
  2. Choose New Directory → New Project.
  3. Enter a folder name, for example my_first_project, and choose where to save it.
  4. Click Create Project.

RStudio creates the folder and a file ending in .Rproj. To reopen the project later, double-click the .Rproj file, or use File → Recent Projects. The current project’s name is shown in the top-right corner of RStudio.

Folder tree: my_project folder containing my_project.Rproj, and data, programs and output folders

A simple project layout
TipA simple project layout

Inside your project folder, create subfolders such as data, programs and output. Then refer to files with relative paths, such as read.csv("data/adsl.csv"). Your code will then work on any computer, as long as the project folder is copied as a whole.

4. Writing and running scripts

Create a script

Go to File → New File → R Script (or press Ctrl + Shift + N). Type:

# My first RStudio script
ages <- c(45, 62, 38, 51, 70)
mean(ages)
summary(ages)
hist(ages, main = "Age distribution", xlab = "Age (years)")

Run code

To run… Windows Mac
The current line, or the lines you’ve selected Ctrl + Enter Cmd + Return
The whole script Ctrl + Alt + R Cmd + Option + R
The whole script, using the Source button Ctrl + Shift + S Cmd + Shift + S

You can also click the Run button at the top of the Source pane.

After running the script above, you’ll see the results in the Console, ages listed in the Environment tab, and a histogram in the Plots tab: all four panes working together.

Save the script

Press Ctrl + S (Cmd + S on a Mac) and give it a name, for example first_script.R. An unsaved script shows its name in red with an asterisk in the tab.

Helpful editor features

  • Code completion: start typing a function or object name and press Tab to see suggestions. Inside a function’s brackets, Tab lists its arguments.
  • Instant help: put your cursor on a function name and press F1 to open its help page.
  • Bracket matching: RStudio adds closing brackets and quotes for you, and highlights matching pairs.
  • Error markers: a red cross in the margin warns you about a syntax error before you run the code.
  • Section headings: a comment ending in four dashes, such as # Load data ----, creates a section you can jump to from the outline (the ≡ button at the top right of the Source pane).

5. Importing data without code

The Environment tab has an Import Dataset button. It can read:

  • From Text (base) or From Text (readr): CSV and other text files
  • From Excel: .xls and .xlsx files
  • From SAS, From SPSS and From Stata: statistical software files

A preview window lets you check the data and the column types. The Code Preview box at the bottom shows the R code that does the import. Copy it into your script, so the import is saved and can be rerun.

For example, importing a SAS dataset generates code like this:

library(haven)
adsl <- read_sas("data/adsl.sas7bdat")
View(adsl)

6. Settings to change on day one

Open Tools → Global Options… (on a Mac, also under RStudio → Settings…).

Don’t save your workspace

Go to General → Basic and:

  • untick “Restore .RData into workspace at startup”
  • set “Save workspace to .RData on exit” to Never

Each session then starts clean, and everything you need comes from your saved scripts. This is the same advice as in the RGui post: it makes your work reproducible, which is essential in regulated pharma work.

Other useful settings

Where Setting Why
Appearance Editor theme and font size A dark theme such as Tomorrow Night is easier on the eyes for long sessions
Code → Editing Use native pipe operator Makes the pipe shortcut insert R’s built-in pipe instead of %>% (needs R 4.1 or later)
Code → Display Use rainbow parentheses Gives each pair of brackets its own colour, which makes nested code easier to read
Pane Layout Choose what goes in each pane Move panes and tabs to suit the way you work

7. Keyboard shortcuts worth learning

Action Windows Mac
Run current line or selection Ctrl + Enter Cmd + Return
Run whole script Ctrl + Alt + R Cmd + Option + R
New R script Ctrl + Shift + N Cmd + Shift + N
Save Ctrl + S Cmd + S
Insert assignment arrow <- Alt + - Option + -
Insert pipe Ctrl + Shift + M Cmd + Shift + M
Comment or uncomment lines Ctrl + Shift + C Cmd + Shift + C
Reformat selected code Ctrl + Shift + A Cmd + Shift + A
Clear the console Ctrl + L Ctrl + L
Move cursor to Source / Console Ctrl + 1 / Ctrl + 2 Ctrl + 1 / Ctrl + 2
Help for function under cursor F1 F1
Show all shortcuts Alt + Shift + K Option + Shift + K

8. Beyond scripts: Quarto documents

RStudio can also create Quarto documents (.qmd), which mix text and R code to produce reports in HTML, Word or PDF. Go to File → New File → Quarto Document…, write your text and code, and click Render.

This blog is itself written in Quarto documents. Reports like this are a great way to share an analysis with its code and results together. We’ll cover Quarto in a future post.

9. RGui and RStudio compared

Task RGui RStudio
Run code Console or script window Console or Source pane
Run current line Ctrl + R Ctrl + Enter
See your objects ls() Environment tab
View a data frame View() in a separate window Click its name in Environment
Plots Separate graphics window Plots tab, with history and Export
Install packages Packages menu Packages tab, or install.packages()
Organise work Set the working folder by hand Projects set it automatically
Code completion and error checking Basic Built in

10. Try it yourself

  1. Create a new project called rstudio_practice.
  2. In the Files tab, create a folder called programs.
  3. Create a new R script and enter:
adsl <- data.frame(
  USUBJID = c("01-001", "01-002", "01-003", "01-004"),
  TRT01A  = c("Placebo", "Drug A", "Placebo", "Drug A"),
  AGE     = c(45, 62, 38, 51)
)

summary(adsl$AGE)
table(adsl$TRT01A)
barplot(table(adsl$TRT01A), main = "Subjects per treatment")
  1. Save it as programs/practice.R and run the whole script.
  2. Find adsl in the Environment tab and click its name to view it.
  3. Export the bar chart from the Plots tab as a PNG file.
  4. Close RStudio, reopen the project from its .Rproj file, and check that your script reopens.

Summary

  • RStudio has four panes: Source, Console, Environment and Files/Plots/Packages/Help.
  • Use a project for each piece of work, so the working folder is always set correctly.
  • Write code in scripts and run it with Ctrl + Enter.
  • Turn off saving and restoring .RData, so every session starts clean.
  • Learn a few shortcuts: Ctrl + Enter, Alt + -, Ctrl + Shift + M and F1.

Next steps

Now that your workspace is set up, the next post covers the building blocks of R: objects, vectors, data types and missing values.