Useful RStudio Shortcuts

R
ggplot
tidyverse
Tips and Tricks in Data Cleaning and Visualization, Part I.
Author

Carlos Fernández

Published

May 25, 2024

Introduction

I recently bought the acclaimed book R for Data Science”(2nd ed.) by Hadley Wichkam et al., and I’ve been finding interesting tidbits that I’d like to share. Most of them are quality-of-life tools that I wasn’t aware of, while others are simply cool tricks.

Note: These shortcuts works for RStudio on Windows. On macOS, replace “Ctrl” with “Cmd”.

Useful shortcuts

The thing with shortcuts is that they don’t really stick to me when I’m starting to learn a new programming language, software tool, or whatever. It’s after I’ve spent hundreds of hours working with them that I stumble upon a small, innocuous shortcut that suddenly makes my life brighter and much easier. Today, I share a few that I’ve encountered in the first chapters of R for Data Science (R4DS for short).

Let’s illustrate them with an example code. I will use the sample datasets that come with the package medicaldata. Specifically, I’ll toy with the dataset called strep_tb, which represent a classic Randomized Controlled Trial on the effects of streptomycin to treat tuberculosis (in fact, it is considered the first modern randomized, placebo-controlled clinical trial).

Show the code
library(medicaldata)
library(tidyverse)
library(here)

data <- medicaldata::strep_tb

glimpse(data)
Rows: 107
Columns: 13
$ patient_id          <chr> "0001", "0002", "0003", "0004", "0005", "0006", "0…
$ arm                 <fct> Control, Control, Control, Control, Control, Contr…
$ dose_strep_g        <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
$ dose_PAS_g          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
$ gender              <fct> M, F, F, M, F, M, F, M, F, M, F, M, F, M, F, M, F,…
$ baseline_condition  <fct> 1_Good, 1_Good, 1_Good, 1_Good, 1_Good, 1_Good, 1_…
$ baseline_temp       <fct> 1_98-98.9F, 3_100-100.9F, 1_98-98.9F, 1_98-98.9F, …
$ baseline_esr        <fct> 2_11-20, 2_11-20, 3_21-50, 3_21-50, 3_21-50, 3_21-…
$ baseline_cavitation <fct> yes, no, no, no, no, no, yes, yes, yes, yes, no, y…
$ strep_resistance    <fct> 1_sens_0-8, 1_sens_0-8, 1_sens_0-8, 1_sens_0-8, 1_…
$ radiologic_6m       <fct> 6_Considerable_improvement, 5_Moderate_improvement…
$ rad_num             <dbl> 6, 5, 5, 5, 5, 6, 5, 5, 5, 5, 6, 5, 5, 5, 5, 6, 5,…
$ improved            <lgl> TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TR…

Alt + - : <-

Here I’ve used the first of the shortcuts, believe it or not: Alt + - to create the assignment arrow <-. Instead of having to type ““, then”<“, then”-“, then”” (4 keys in total), now just with Alt + - (2 keys simultaneously) I can get the job done, because spacing is added automatically. A silly little trick, but very useful, especially when I’m working with a keyboard that doesn’t have the < or > keys in the same place as others.

Ctrl + Up : List All Similar Commands in the Console’s History

This I can’t show you with code, but with a screenshot of RStudio’s console. If you press Ctrl + Up arrow, you can see a list of previous commands that start with the same words. You can access commands that were typed even in previous R sessions working with different files. I find it useful to trace back to some command that I want to replicate.

Ctrl + Up lets you look at your Console’s history of similar commands.

F1 When the Autocomplete Yellow Box Pops Up : Access the Help File of That Function

How many times have I tried to remember the specific syntax of a function and had to go back and forth between the actual code and typing help() or ?function ? There was an easier way: just press F1 When the little yellow box of autocomplete appears while typing the function. It makes the R Documentation file appear in the RStudio help panel, and I can look into it without even needing to stop from coding.

F1 when the autocomplete yellow box pops up…

…the R Documentation help file appears right away in RStudio.

Ctrl + Shift + R: Create Sections in Your Code

One way to organize your R code more neatly is to use sections and sub-sections. To create a section, start with a comment (# followed by text), and end with a sequence of four or more hyphens or hashtags (---- or ####). That will tell RStudio that that particular line of text is a section, and you can jump between sections using the R editor window.

The shortcut Ctrl + Shift + R pops up a window for you to enter the label of the section, and a new section appears, saving you the trouble of typing all those hyphens manually.

Ctrl + Shift + R makes creating sections easy and fast.
Show the code
# This is a section -------------------------------------------------------

Other Shortcuts

Lastly, I’ll share some other useful shortcuts:

  • Ctrl + Shift + P: Open the “command palette” of RStudio, useful to find every command the IDE and its add-ins (like the “styler” package) have to offer, including their own shortcuts.

  • Ctrl + Shift + S: Runs the whole script.

  • Ctrl + Shift + F10: Restart the R session.

That’s all for now. See you next time!

References