Chapter 6 Tips and tricks
Learning goals
By the end of this chapter you should be able to:
- Adjust RStudio so it is comfortable to use.
- Use a few high‑value keyboard shortcuts.
- Export plots and tables cleanly.
- Avoid common data‑import problems.
Prerequisites
- You can run code from a script in RStudio.
A tiny motivating example
Here is a small quality‑of‑life improvement you can apply immediately:
This strongly discourages R from using scientific notation when printing numbers.
6.1 Appearance
What we’re about to do: Make RStudio easier to read.
You can change the appearance under Tools > Global Options > Appearance (Windows and Linux) or RStudio > Preferences > Appearance (macOS). There you can change the theme and font.
- Use a monospace font (e.g., Source Code Pro, Menlo, or Consolas).
- Increase font size if you are squinting.
Takeaway: A readable setup reduces mistakes and fatigue.
6.2 Shortcuts (top six)
What we’re about to do: Learn the shortcuts you will use every session.
- Run current line/selection:
Ctrl+Enter(macOS:Cmd+Enter) - Run from start to current line:
Ctrl+Alt+B(macOS:Cmd+Option+B) - Comment/uncomment:
Ctrl+Shift+C(macOS:Cmd+Shift+C) - Find in files:
Ctrl+Shift+F(macOS:Cmd+Shift+F) - Auto-complete: start typing, then press
Tab - Insert the pipe operator
|>:Ctrl+Shift+M(macOS:Cmd+Shift+M)
This course uses R’s native pipe, |>. You may also encounter the older %>% pipe in other code. For everything we do in this course, you can treat them as equivalent. Their differences matter only in more specialised code. If the shortcut inserts %>%, change the pipe setting under Tools > Global Options > Code > Editing (Windows and Linux) or RStudio > Preferences > Code > Editing (macOS).
Try this: Use Ctrl+Shift+C to comment out a block, then toggle it back.
Takeaway: Shortcuts save time and reduce clicking.
6.3 Code style (keep it readable)
What we’re about to do: Make your scripts easier to read.
A few habits help a lot:
- One pipe step per line.
- Clear, meaningful object names.
- Blank lines between sections.
If you want a full guide, the tidyverse style guide is excellent: https://style.tidyverse.org.
Takeaway: Readable code is easier to debug and reuse.
6.4 The plots pane
What we’re about to do: Save plots cleanly.
Plots appear in the Plots pane (usually bottom right). Use the back/forward arrows to browse old plots and Export to save one. In the export window, choose suitable dimensions and check the preview before saving.
Common mistake: Exporting a plot without checking its dimensions. A plot that looks fine in the pane may become cramped when saved at a different size.
6.5 Tables
If you need to use a summary table outside R, save it as a CSV file and open it in spreadsheet software such as Excel.
6.6 Importing data: common problems
What we’re about to do: Avoid the most common import errors.
- Everything in one column? You probably used the wrong delimiter.
- Unexpected symbols? The file may use a different text encoding. Inspect it in a text editor and check how it was saved.
Sanity checks:
str(data)to check column typesnames(data)to check column namesnrow(data)andncol(data)to check size
Takeaway: Always inspect data right after importing.