Overview
RStudio is a free, open-source integrated development environment (IDE) for the R language, made by the company Posit (formerly named RStudio, PBC). RStudio does not replace R; it runs on top of R and gives you a much better place to work: a code editor with auto-completion, a console, a view of your data and variables, and panes for charts, files, packages, and help.
In this guide you will install RStudio Desktop, learn its layout, and complete a beginner example: creating a project, writing a script, and making a chart.
Requirements
- R already installed. See the R guide if you haven't installed it.
- A supported operating system: Windows 11, macOS 15 or later, or a recent 64-bit Linux distribution such as Ubuntu, Debian, or Fedora.
- Administrator privileges to install software.
- Internet access.
Important Notes
- If RStudio says it can't find R, R is not installed or was installed after RStudio was opened. Install R, then restart RStudio.
- Posit also makes Positron, a newer free IDE for both R and Python. This guide covers RStudio, which is still the most widely used R editor.
RStudio Installation Instructions
Choose the tab for your operating system:
Installing RStudio Desktop
Make sure R is installed first, then click the tab above for your operating system.
The guide covers these major tasks:
- Download RStudio Desktop.
- Run the installer.
- Open RStudio.
- Confirm it found R.
Confirm the installation
When RStudio opens, the Console pane shows R's startup message with the R version number. If you are asked to choose an R installation, select the R 4.6 version you installed.
R.version.string in the Console and press Enter. RStudio reports your R version.
The RStudio Layout
RStudio is divided into four panes:
| Pane | Default position | What it does |
|---|---|---|
| Source (editor) | Top left | Write and save R scripts (.R), Quarto and R Markdown documents. Appears when you open or create a file. |
| Console | Bottom left | Runs R commands and shows the results. Also has Terminal and Background Jobs tabs. |
| Environment / History | Top right | Lists your variables and data frames. Click a data frame to view it as a table. |
| Files / Plots / Packages / Help / Viewer | Bottom right | Browse files, see charts, install and load packages, and read help pages. |
You can change the theme and font size in Tools → Global Options → Appearance.
Beginner Example: A Project, a Script, and a Chart
Step 1: Create a project
An RStudio project keeps all the files for one piece of work together in a folder, and RStudio remembers it for you.
- Choose File → New Project.
- Choose New Directory, then New Project.
- Enter a Directory name such as
first-r-project, choose where to create it (for example, Documents), and click Create Project.
RStudio reopens inside the project. Its name appears at the top right.
Step 2: Install a package
The ggplot2 package makes professional charts. In the Console, run:
install.packages("ggplot2")
You only need to install a package once per computer. Wait until the > prompt returns.
Step 3: Write a script
Choose File → New File → R Script. Type the following into the new script:
# first-analysis.R
library(ggplot2)
# Look at the built-in mtcars data
head(mtcars)
summary(mtcars$mpg)
# Average miles per gallon for each number of cylinders
aggregate(mpg ~ cyl, data = mtcars, FUN = mean)
# Scatter plot: weight vs. fuel economy, colored by cylinders
ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl))) +
geom_point(size = 3) +
geom_smooth(method = "lm", se = FALSE) +
labs(title = "Heavier Cars Get Fewer Miles per Gallon",
x = "Weight (1000 lbs)",
y = "Miles per Gallon",
color = "Cylinders")
Save the script with File → Save (or Ctrl + S / Cmd + S) as first-analysis.R. It appears in the Files pane.
Step 4: Run the code
- To run one line (or a selection), put your cursor on it and press Ctrl + Enter (Windows/Linux) or Cmd + Enter (Mac).
- To run the whole script, click Source at the top of the editor, or press Ctrl + Shift + Enter / Cmd + Shift + Enter.
Run the script line by line. Results appear in the Console, and the chart appears in the Plots pane. The aggregate line shows average mpg of about 26.7 for 4-cylinder cars, 19.7 for 6-cylinder cars, and 15.1 for 8-cylinder cars.
Step 5: Explore and export
- In the Console, run
cars_data <- mtcars. The new data frame appears in the Environment pane; click its name to view it as a sortable table. - In the Plots pane, click Export → Save as Image to save the chart as a PNG file.
cars_data is listed in the Environment pane.
Common Errors
- there is no package called 'ggplot2': the package isn't installed yet. Run
install.packages("ggplot2"). - could not find function "ggplot": the package is installed but not loaded. Run
library(ggplot2)first. - object not found: check the spelling and capitalization of the variable name.
Next Steps
- Create a Quarto document (File → New File → Quarto Document) that mixes text, code, and charts into a report.
- Connect RStudio to Git and GitHub with Tools → Version Control.
- Read the RStudio User Guide.
- RStudio is an IDE that runs on top of R; install R first.
- Use projects to keep each analysis in its own folder.
- Write code in scripts so you can save and rerun it, and use Ctrl/Cmd + Enter to run lines.
- Install packages once with
install.packages(); load them in each script withlibrary().