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. The setting lasts only for the current R session, so put it at the top of your script if you want it every time.
6.1 Appearance
What we’re about to do: Make RStudio easier to read.
You can change the appearance under Tools > Global Options > Appearance. There you can change the theme, the font and the font size.
- Increase the font size if you are squinting.
- Try a dark theme if you find a white background tiring.
Takeaway: A readable setup reduces mistakes and fatigue.
6.2 Shortcuts (top seven)
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) - Insert the assignment operator
<-:Alt+-(macOS:Option+-) - Restart R (a clean session):
Ctrl+Shift+F10(macOS:Cmd+Shift+0) - Auto-complete: start typing, then press
Tab - Insert a pipe:
Ctrl+Shift+M(macOS:Cmd+Shift+M)
This book uses the %>% pipe. You will also see R’s built-in (“native”) pipe, |>, in other people’s code. For everything we do in this course, you can treat them as equivalent: x %>% f() and x |> f() both mean f(x). Their differences matter only in more specialised code.
Which pipe the shortcut inserts depends on your RStudio version and settings: recent versions (2026.06 onwards) insert |> by default, older ones %>%. Either is fine. If you want the shortcut to match the book, untick Use R’s native pipe operator, |> under Tools > Global Options > Code > Editing. (In the newest versions, the same shortcut inserts + when the cursor is inside a ggplot() chain.)
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.
For a plot you will need again, or want at an exact size (for example in a report), save it from code with ggsave() instead, so the size is recorded in your script. The Pimping your plots chapter shows how.
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.
Danish Excel: if Excel uses Danish settings, it expects ; between columns and , as the decimal mark, so a file made with write.csv() may open with everything in one column. Use write.csv2() instead to write a file in that format.
6.6 Importing data: common problems
What we’re about to do: Avoid the most common import errors.
- Everything in one column? The file probably uses a different delimiter. CSV files saved from Excel with Danish settings use
;between columns and,as the decimal mark: read them withread.csv2()(orread.csv(..., sep = ";")). - Strange characters instead of æ, ø, å? The file uses a different text encoding. Try
read.csv(..., fileEncoding = "UTF-8")orfileEncoding = "latin1". - A strange first column name, such as
X.U.FEFF.Nameorï..Name? The file starts with an invisible “byte order mark”. Useread.csv(..., fileEncoding = "UTF-8-BOM"). - Missing values coded as numbers, such as
-999? Tell R withna.strings, for exampleread.csv(..., na.strings = "-999"). Otherwise these values are included in means and plots. - A numeric column imported as text or a factor? At least one value is not a number (a typo, a space, a word such as “unknown”). See the
carnivoraexample in An R refresher.
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.