1  Keyboard shortcuts and navigation

An under-appreciated but powerful aspect of your work as a data science is being adept with your keyboard. This includes knowing useful keyboard shortcuts and being able to do stuff with your cursor without touching your mouse.

Why am I asking you to learn these? Two reasons!

1.2 RStudio keyboard shortcuts

To view all of RStudio’s keyboard shortcuts, navigate to:

  • Help > Keyboard Shortcuts Help

You can also customize these shortcuts:

  • Tools > Modify Keyboard Shortcuts…

Below are (IMO) the most useful RStudio keyboard shortcuts.

1.2.1 Tab completion

Type part of a function or object name (in the Editor or Console) and then hit Tab. A menu of autocomplete options will popup. Select your choice with arrow keys and hit Tab or Enter. (e.g., Type ggp and hit Tab.) Tab completion also works for function arguments.

1.2.2 Working in source files

Action Windows Windows free icon Mac Apple free icon
Insert code chunk Ctrl Alt I Option Command I
Insert pipe operator (%>%) Ctrl Shift M Command Shift M
Insert assignment operator (<-) Alt - Option -
Run current code chunk Ctrl Shift Enter Command Shift Return
Run current line/currently selected lines Ctrl Enter Command Return
(Un)comment current line/currently selected lines Ctrl Shift C Command Shift C

Notes:

  • On my Mac, I like to change the “Insert assignment operator” shortcut to Command , (This overrides the shortcut for pulling up RStudio’s options, but I don’t look at those options often anyway.)
  • When running a single line of code, your cursor can be anywhere on the line–it doesn’t need to be all the way at the beginning or end.
  • When running code that is selected across multiple lines, exactly what is selected is run. If you want to run part of a line, select just that part. If you want to run multiple whole lines, select all those lines in their entirety.
  • When commenting/uncommenting lines, you don’t need to have the whole line fully selected. Having just part of a line selected suffices.

1.2.3 Working in the Console

While you may be used to writing all of your code in code chunks within a document, it is often useful to run code interactively in the Console as “scratch work.” You’ll often find that when testing out code, trying it out in the Console keeps your code document clean.

Action Windows Windows free icon Mac Apple free icon
Move cursor to the Console Ctrl 2 Ctrl 2
Cycle through previous commands Up/Down Up/Down
Show pop up of command history Ctrl Up Command Up
Interrupt currently executing command Esc Esc

You can pull up the documentation (help page) for a function by entering ?function_name in the Console.