How to use RStudio — the four panes, running line by line, seeing your data and asking for help (and the workspace that brought me variables from another lab)
RStudio has four panes that work at once: if you know what each one does and how to pass code from one to another, the ME1 labs take half as long.
In the second lab I opened RStudio and the variables pane was already full: data, mean_value, x… everything from the previous lab. It seemed handy, until my script worked perfectly on my laptop and failed on my classmate’s with an object ‘data’ not found. My code used a variable I had never created in that script: it came from the «workspace» saved the week before. There is a box in the settings that prevents this, and it is the first thing I recommend you change.
In this guide we go through RStudio pane by pane with real screenshots: the editor and the console, how to run line by line, the variables and the data viewer, plots, help and autocompletion, the working folder and the settings that save you trouble. If you have not installed it yet, start with Install R and RStudio.
Table of Contents
The window at a glance
With a script open and run, RStudio looks like this. It is the test script from the installation guide (the Spanish version, so the names are in Spanish): some marks, a summary, the mean per group and a histogram.

And this is the map of what each pane does:
| Pane | Where | What for |
|---|---|---|
| Source (editor) | Top left | Writing and saving .R scripts |
| Console | Bottom left | Where R runs and answers |
| Environment | Top right | The variables that exist right now |
| Files, Plots, Packages, Help | Bottom right | Files, plots, packages and help |
You can move the panes around in Tools > Global Options > Pane Layout, but I recommend leaving them like this: it is the layout you will see in every tutorial and on the lab computers.
The editor and the console: writing and running
The golden rule of the labs: everything that matters goes in a script, not straight into the console. What you type in the console is lost when you close; a script is saved, can be run again in full and can be handed in.
- Create a script: File > New File > R Script, or Ctrl + Shift + N.
- Save it: Ctrl + S. While it has unsaved changes, its name appears in red on the tab.
- Run the line where the cursor is: Ctrl + Enter. The line goes to the console, R runs it and the cursor moves down to the next one. If you select several lines, they all run.
- Run the whole script: Ctrl + Shift + Enter, or the Source button on the editor toolbar.
Ctrl + Enter is the shortcut you will use most. It lets you go line by line, look at what came out and carry on, which is exactly what you need when something does not add up.
If the console shows a + instead of >, R is waiting for you to finish the command (you are missing a closing bracket or quote). Press Esc in the console to cancel it and run the corrected line again.
You can also type directly in the console to try something quickly. Two tricks: the up arrow brings back previous commands, and Ctrl + L clears the console (only the screen: the variables are still there).
Environment: your variables and the data viewer
The Environment pane shows everything that exists right now in the R session, with its type and a preview of its value:

At the top, under Data, are the tables (data frames) with their size: 8 obs. of 2 variables means 8 rows and 2 columns. Below, under Values, the rest: numbers, vectors (num [1:8] is a numeric vector of 8 values) and text (chr).
If you click the table icon to the right of a data frame (or type View(data) in the console), it opens in the data viewer, in an editor tab:

The viewer only shows the data: if you change something in it, the variable does not change. For that, code.
The broom in the Environment pane deletes all the variables in the session. Use it before running your whole script to check that it works from scratch, without depending on anything you created by hand.
Next to Environment is the History tab, with every command you have run. It is useful to rescue something you tried in the console and want to move into the script.
Plots: seeing and saving plots
Every plot you make (hist, plot, boxplot…) appears in the Plots pane:

- The arrows on the left go through the plots you have made in the session.
- Zoom opens the plot in a big window (useful if the pane is small).
- Export saves it as an image or PDF, or copies it to the clipboard to paste into your lab report:

The plot fails with the error figure margins too large. It is not a bug in your code: the Plots pane is too small for the plot. Make the pane bigger by dragging its edge, or use Zoom.
Help and autocompletion
RStudio has R’s documentation built in. Type ?pnorm (or help(pnorm)) in the console, or put the cursor on a function name in the editor and press F1:

The help is a bit intimidating at first, but it always has the same structure. The most useful parts are Usage, which tells you the arguments and their default values (here, that pnorm has mean = 0 and sd = 1 unless you say otherwise, and that lower.tail = TRUE gives the cumulative probability from the left), and Examples, at the end, with code you can copy and run.
While you type, autocompletion suggests functions and variables. It appears on its own after three letters, or when you press Tab:

Besides saving you typing, it prevents typos: if the function does not appear in the list, you have probably misspelt it or its package is not loaded.
Files and the working folder
R reads and saves files in its working directory. If you read read.csv("data.csv") and the file is not in that folder, it fails with cannot open file. To find out which folder it is, getwd() in the console.
The quick way to change it is the Session > Set Working Directory menu:

- To Source File Location: the folder where the script you have open is saved. If you keep the script next to its data, this is the right option.
- To Files Pane Location: the folder shown in the Files pane.
- Choose Directory… (Ctrl + Shift + H): you pick the folder.
Even better: create a project for the subject (File > New Project…). A project is a folder with an .Rproj file; when you open it, RStudio sets the working folder there automatically, and each project has its own open scripts. An «ME1» folder with a project and each lab’s data inside saves you all the path problems.
Writing the path with the Windows backslash: read.csv("C:\Users\ana\data.csv") gives an error, because in R the \ inside a string is used to escape characters. Use the normal slash, "C:/Users/ana/data.csv", or a double backslash, "C:\\Users\\ana\\data.csv". Or, better, a project and relative paths: read.csv("data.csv").
The settings that save you trouble
They are in Tools > Global Options. The General section has the setting from the story at the beginning:

By default, RStudio saves all your variables in an .RData file when you close it (or asks whether you want to) and loads them again when you open it. It looks handy, but it is the origin of my story:
The variables from lab 1 «survive» and your lab 2 script can use them without you noticing. It works on your computer and fails on any other. To cut it off at the root:
- Untick Restore .RData into workspace at startup.
- In Save workspace to .RData on exit, choose Never.
- Click OK.
With that, every time you open RStudio you will start with an empty Environment, and what is in your script will be the only thing that exists. If you ever get this warning when closing, you know what to answer: Don’t Save.

What you should save are the scripts (Ctrl + S). With the script, getting the variables back is as easy as running it again: Ctrl + A and Ctrl + Enter.
In Appearance you can change the editor theme (dark ones are easier on the eyes), the font and its size, and the zoom of the whole window:

And Pane Layout has the pane arrangement I mentioned at the beginning:

Keyboard shortcuts
The ones that save the most time in the labs. On a Mac, Ctrl is Cmd:
| Shortcut | What it does |
|---|---|
| Ctrl + Enter | Runs the line (or the selection) and moves to the next |
| Ctrl + Shift + Enter | Runs the whole script |
| Ctrl + Shift + N | New script |
| Ctrl + S | Saves the script |
| Alt + − | Types the assignment arrow <- |
| Ctrl + Shift + M | Types the pipe |> |
| Ctrl + Shift + C | Comments or uncomments the selected lines |
| Tab | Autocompletes |
| F1 | Help for the function under the cursor |
| Ctrl + L | Clears the console |
| Ctrl + Shift + F10 | Restarts R (clean session) |
| Alt + Shift + K | The full list of shortcuts |
Ctrl + Shift + F10 restarts R without closing RStudio. Then Ctrl + A and Ctrl + Enter run your script from scratch. It is the best test before handing in a lab.
Typical problems and how to fix them
| Symptom | Likely cause | Fix |
|---|---|---|
object 'data' not found on another computer, but it works on yours | Your script uses a variable from the saved .RData | The Workspace settings above, and test the script after restarting R |
The console shows + and does nothing | A bracket or quote is missing | Esc in the console and fix the line |
cannot open file 'data.csv' | The file is not in the working folder | getwd() and Session > Set Working Directory, or a project |
could not find function "fitdistr" | The package is not loaded | library(MASS) at the top of the script |
| figure margins too large | The Plots pane is small | Make the pane bigger or use Zoom |
| The script name appears in red | It has unsaved changes | Ctrl + S |
| The editor pane is not there | No script is open | Ctrl + Shift + N or open one with Ctrl + O |
Summary
- Four panes: editor top left, console below, variables top right and files, plots and help bottom right.
- Everything in a script and Ctrl + Enter to go line by line; Ctrl + Shift + Enter for all of it.
- Environment shows your variables; the table icon (or
View()) opens the data viewer. - Plots saves your plots with Export; Help (
?functionor F1) explains any function. - Working folder: one project per subject and relative paths.
- Turn off the workspace (Restore .RData and Save set to Never): your script has to work from scratch.
With RStudio under control, it is time to learn R properly: R from scratch starts with the console as a calculator, vectors and matrices, and the second part carries on with data frames, reading files and functions. And if one day you do not have RStudio at hand, the site’s R playground does almost everything in this guide in the browser: running line by line with Ctrl + Enter, View(), ?pnorm, plots and the variables pane.
Did this article help you? Here is how you can support SergioLearns →



