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.

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.

RStudio with its four panes: a script in the editor, the console with summary and tapply, the Environment pane with five variables and the histogram in Plots
RStudio with a script run: editor, console, variables and plot.

And this is the map of what each pane does:

The four panes of RStudioEditor top left, console bottom left, Environment top right and Files, Plots, Packages and Help bottom right.Editor (Source)your .R scripts: write hereand save (Ctrl + S)Ctrl + Enter sends the line belowConsoleR runs here and results appearand errors; > waits for a commandEnvironmentthe variables that existright now; the table iconopens the data viewerFiles · Plots · Packages · Helpyour files, the plots,the packages and the helpCtrl + Enter
What you run in the editor goes to the console; what it creates appears in Environment and its plots in Plots.
PaneWhereWhat for
Source (editor)Top leftWriting and saving .R scripts
ConsoleBottom leftWhere R runs and answers
EnvironmentTop rightThe variables that exist right now
Files, Plots, Packages, HelpBottom rightFiles, 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:

RStudio Environment pane enlarged: under Data, a data frame with 8 obs. of 2 variables; under Values, six variables with their type and first values
Environment separates tables (Data) from the other values (Values).

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:

RStudio data viewer with a table of two columns, nota and grupo, and 8 numbered rows
The data viewer: you can sort by clicking a column and filter with Filter.

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:

Plots pane enlarged with a histogram of exam marks: violet bars between 3 and 9 and a vertical coral line at the mean
Plots with the marks histogram and the line at the mean.
  • 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:
Export menu of the Plots pane open with the options Save as Image, Save as PDF and Copy to Clipboard
Export: Save as Image…, Save as PDF… and Copy to Clipboard…

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:

Help pane with the page The Normal Distribution: the description and the Usage section with dnorm, pnorm, qnorm and rnorm
The help for pnorm: at the top, what it does; under Usage, the arguments with their default values.

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:

RStudio console with pno typed and the autocompletion list showing pnorm from the stats package next to a card with its usage and description
With pno, RStudio suggests pnorm and shows its usage without opening the help.

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:

RStudio Session menu with the Set Working Directory submenu open: To Project Directory, To Source File Location, To Files Pane Location and Choose Directory
Session > Set Working Directory: the most practical option is To Source File Location.
  • 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:

Global Options window in the General section: R version, default working directory and, under Workspace, the Restore .RData into workspace at startup box ticked and Save workspace to .RData on exit set to Ask
Global Options > General. Under Workspace is the box worth changing.

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 .RData workspace cycleThe variables from lab 1 are saved in .RData on closing and reappear when opening lab 2; the fix is to neither save nor restore the workspace.Lab 1you create x, data, mean…Close RStudio"Save workspace?" → Save.RDataall your variables, in a fileLab 2x and data are backThe fixGlobal Options >General:untick "Restore.RData into workspace"and "Save workspace"to Never
Your script must run from scratch. If it depends on variables that "were already there", it will fail when someone else runs it.

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:

  1. Untick Restore .RData into workspace at startup.
  2. In Save workspace to .RData on exit, choose Never.
  3. 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.

RStudio Quit R Session dialog asking whether to save the workspace image to an .RData file, with the Save, Don't Save and Cancel buttons
The warning when closing: with the settings above it will not appear again.

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:

Global Options in the Appearance section: RStudio theme, zoom, editor font and its size, and the list of editor themes with a preview of R code
Appearance: theme, font and zoom. The preview on the right changes when you choose a theme.

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

Global Options in the Pane Layout section: Source top left, Console bottom left, Environment and History top right, and Files, Plots, Packages, Help and Viewer bottom right
Pane Layout: which tabs go in each pane.

Keyboard shortcuts

The ones that save the most time in the labs. On a Mac, Ctrl is Cmd:

ShortcutWhat it does
Ctrl + EnterRuns the line (or the selection) and moves to the next
Ctrl + Shift + EnterRuns the whole script
Ctrl + Shift + NNew script
Ctrl + SSaves the script
Alt + −Types the assignment arrow <-
Ctrl + Shift + MTypes the pipe |>
Ctrl + Shift + CComments or uncomments the selected lines
TabAutocompletes
F1Help for the function under the cursor
Ctrl + LClears the console
Ctrl + Shift + F10Restarts R (clean session)
Alt + Shift + KThe 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

SymptomLikely causeFix
object 'data' not found on another computer, but it works on yoursYour script uses a variable from the saved .RDataThe Workspace settings above, and test the script after restarting R
The console shows + and does nothingA bracket or quote is missingEsc in the console and fix the line
cannot open file 'data.csv'The file is not in the working foldergetwd() and Session > Set Working Directory, or a project
could not find function "fitdistr"The package is not loadedlibrary(MASS) at the top of the script
figure margins too largeThe Plots pane is smallMake the pane bigger or use Zoom
The script name appears in redIt has unsaved changesCtrl + S
The editor pane is not thereNo script is openCtrl + 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 (?function or 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.

Keep goingAll Tools articles

Did this article help you? Here is how you can support SergioLearns →