What Is RStudio? Run Your First R Analysis with Projects and Scripts
RStudio is an IDE for writing and running R code. Learn how it differs from R, organize work in a project, run a script, inspect results, and repeat the analysis with the built-in mtcars dataset.
RStudio is an integrated development environment (IDE) for writing R code and inspecting its results. R is the language and runtime that performs the analysis; RStudio brings a script editor, console, and tools for viewing data and plots into one workspace. This guide walks through a first workflow in RStudio Desktop: create a project, run an R script, and inspect its table and plot.
How are R and RStudio different?
R is a programming language and runtime for statistical computing and data analysis. RStudio is not a replacement language; it is an IDE for working with R more conveniently. That is why using RStudio Desktop generally also requires a separate R installation. Posit's installation guide says R must be installed separately. [1]
The default RStudio workspace has four areas. You can rearrange the panes, so the locations below describe the common desktop layout. [2]
| Area | What it does | Example in an analysis |
|---|---|---|
| Source | Write and edit scripts and analysis documents that you can save | Record the analysis steps and notes in summary.R |
| Console | Run commands in the current R session and read messages and results | Run one line or inspect an error |
| Environment | See R objects created in the current session | Check whether mtcars and cylinder_means are in memory |
| Output | View results and supporting panels such as files, plots, and help | Inspect a scatterplot in Plots |
When you run a line or selected lines in the editor, R executes those commands in the Console and displays the output there. Use the Source button to run a whole script. On Windows and Linux, Ctrl+Enter usually runs the current line or selection; on macOS, use Cmd+Enter. You can also find the shortcuts in the Run menu. [3]
Why create a project first?
An RStudio Project groups the input files, scripts, plots, and output files for one analysis in a work folder, and uses that folder as the working-directory reference. Opening its .Rproj file reopens the project and sets the working directory to the project folder. This reduces hard-coded machine-specific absolute paths and makes an analysis easier to rerun or share. [4]
Choose File > New Project > New Directory > New Project, then name the folder something like mtcars-exploration. After it is created, check that the project name appears in the project selector at the top of the window. To start from an existing folder, choose the existing-directory option in the same wizard. [4]
You can organize files in subfolders such as data for source data, scripts for code, and output for results. Those names are only conventions. The important part is keeping the inputs and code for one analysis together and using paths relative to the project. Before copying personal files into a project or pushing it to a shared repository, check permissions and sensitive data.
Make a table and plot from a script
Simply opening RStudio does not save an analysis procedure. Typing directly in the Console is useful for quick checks, but to explain and rerun an analysis, save an ordered script in the Source editor. This example uses mtcars, a dataset included with R. It contains fuel consumption and design/performance variables for 32 cars from 1973–74, so no additional package is needed. It is historical data and should not be treated as representative of today's vehicles. [5]
Create a script with File > New File > R Script, paste the code below, and save it as summary.R. Put the cursor on a line and click Run, or select a region and run it. Click Source to run the complete script in order. [3]
data("mtcars")
head(mtcars)
str(mtcars)
cylinder_means <- aggregate(
mpg ~ cyl,
data = mtcars,
FUN = mean
)
print(cylinder_means)
plot(
mtcars$wt,
mtcars$mpg,
xlab = "Weight (1000 lb)",
ylab = "Miles per US gallon",
main = "Weight and fuel economy in mtcars"
)head() shows the first rows, and str() summarizes columns and data types. aggregate(mpg ~ cyl, ...) groups rows by cyl and calculates the mean mpg in each group. In the formula interface to aggregate, the left side is the variable to summarize and the right side is the grouping variable; FUN specifies the summary function. The resulting table contains the grouping variable and the aggregate column. [6]
If the script runs, cylinder_means should contain three rows, one for each cylinder-count group, and the scatterplot should display each car's weight (wt, in 1,000 pounds) against fuel economy (mpg, miles per US gallon). Find cylinder_means in the Environment pane and the plot in the Output pane's Plots tab. This table and plot are starting points for exploration. mtcars is a small historical dataset, and a pattern between two variables does not establish that weight caused fuel economy to change. [5]
Use the script as the record, not the session's numbers
Objects shown in the Environment live in the current R session's memory and may disappear when R restarts. A saved .R script remains as a file, so you can rerun it from the top together with its required input data. Posit's getting-started guide recommends treating scripts, rather than a saved workspace, as the reproducible record of an analysis. [1]
To check basic reproducibility, reopen the project and Source summary.R; confirm that it creates the same table and plot. This example requires no package installation. In another project, packages are generally installed into an R library with install.packages("package_name") and loaded in each new session with library(package_name). Packages are installed into environments that depend on the computer or R version, so collaborators should document and align their R and package dependencies.
This distinction matters in practice. Temporary objects in the Environment or commands previously entered in the Console are not enough to reproduce an analysis. Save the steps for reading data, checking variables and missing values, and producing results in code files so they can be reviewed in a new session. Automatically restoring .RData can be convenient, but it can also encourage scripts that depend on hidden objects from an earlier session. Starting without restoring the workspace and checking that the script works from the beginning helps expose those dependencies. [1][4]
When execution or file lookup gets stuck
RStudio opens, but the Console does not start: Confirm that R is installed; Posit's RStudio download guide says R is a separate installation. If you have multiple R versions, check which one RStudio selected and whether it matches the project, then restart the IDE. Follow Posit's current download instructions for your platform. [7]
`object not found`: You may have run code before the line that creates the object, or restarted R and emptied the Environment. `Source` the script from the top and inspect the Console to find the failing line.
A file cannot be found: Use `getwd()` to check the current working directory and compare it with the project and file locations. Confirm that the project is open and that the relative path, filename capitalization, and extension are correct. Organizing inputs under the project usually works better on another computer than fixing an absolute path. [4]
No table or plot appears: Check whether you ran only the line under the cursor, a selection, or the entire file. Look for the table in Console output or the object viewer, and the plot in the `Plots` tab. If execution stopped on an error, resolve it before expecting later output. [2][3]
Several people are changing the same analysis: RStudio's Git integration can show changes, compare differences, and commit within a project when Git is installed and enabled. It does not replace Git itself; a remote repository and authentication are separate. Before committing, check that shared files contain no secret keys or personal data. [8]
The key to using RStudio well is not memorizing four panes. It is grouping each analysis in a project, saving the execution order in the Source editor, and checking results in the Console and Output pane. That workflow separates temporary session state from an analysis procedure that can be run again.
References
Posit, Get Started with RStudio: <https://docs.posit.co/ide/user/ide/get-started/index.html> — R and RStudio installation, interface, projects, session objects, and scripts.
Posit, Pane Layout: <https://docs.posit.co/ide/user/ide/guide/ui/ui-panes.html> — default panes and their roles.
Posit, Executing Code: <https://docs.posit.co/ide/user/ide/guide/code/execution.html> — running lines, selections, and complete files from the editor.
Posit, RStudio Projects: <https://docs.posit.co/ide/user/ide/guide/code/projects.html> — project creation, working directory, and options.
R Documentation, `mtcars`: <https://stat.ethz.ch/R-manual/R-devel/library/datasets/html/mtcars.html> — dataset source, observation count, and variables.
R Documentation, `aggregate`: <https://stat.ethz.ch/R-manual/R-devel/library/stats/html/aggregate.html> — grouped summaries and formula interface.
Posit, Download RStudio: <https://docs.posit.co/ide/user/download.html> — current platform download guidance and separate R installation.
Posit, Version Control: <https://docs.posit.co/ide/user/ide/guide/tools/version-control.html> — Git/Subversion requirements and project integration.