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:

options(scipen = 999)

This tells R to avoid scientific notation when printing numbers.


6.1 Appearance

What we’re about to do: Make RStudio easier to read.

You can change the appearance in Tools > Options (Windows) or RStudio > Preferences (Mac). The Appearance tab lets you change theme and font.

  • Use a monospace font (e.g., Menlo, Courier, Lucida Console).
  • 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 (Mac: Cmd+Enter)
  • Run from start to current line: Ctrl+Alt+B (Mac: Cmd+Option+B)
  • Comment/uncomment: Ctrl+Shift+C
  • Find in files: Ctrl+Shift+F
  • Auto‑complete: type, then use arrows + Enter
  • Insert pipe %>%: Ctrl+Shift+M (Mac: Cmd+Shift+M)

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. Use Export to save a plot.

Common mistake: Exporting before resizing. If your plot looks cramped, resize the plot pane first.


6.5 Tables

If you create a summary table, save it to CSV and open in Excel or Word.

write.csv(my_table, file = "example.csv", row.names = FALSE)

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.
  • Weird symbols? Open the file in a text editor and inspect it.

Sanity checks:

  • str(data) to check column types
  • names(data) to check column names
  • nrow(data) and ncol(data) to check size

Takeaway: Always inspect data right after importing.


6.7 Key takeaways

  • Make RStudio comfortable and readable.
  • Learn a few shortcuts and use them daily.
  • Export plots and tables deliberately.
  • Always check imported data.

6.8 Common pitfalls recap

  • Forgetting to check the delimiter when importing.
  • Using non‑monospace fonts that misalign code.
  • Skipping basic checks after import.

6.9 Mini‑project

Pick one dataset you have imported and:

  1. Check it with str() and summary().
  2. Save a small summary table to CSV.
  3. Export one plot and write a sentence describing it.