Tips and tricks
December 20 2022
tips.Rmd
General
-
Keyboard shortcuts:
Description Mac Windows Run lines of code Command + Enter
Ctrl + Enter
(Un)comment lines of code Command + Shift + C
Ctrl + Shift + C
Insert a foldable comments section Command + Shift + R
Ctrl + Shift + R
Reindent lines of code Command + I
Ctrl + I
Reformat lines of code Command + Shift + A
Ctrl + Shift + A
Attempt code completion Tab
Tab
Clear the console Control + L
Ctrl + L
Insert an assignment operator, <-
Alt + -
Alt + -
Insert a pipe operator, %>%
Command + Shift + M
Ctrl + Shift + M
Rename a variable in scope Command + Shift + M + Alt
Ctrl + Shift + M + Alt
Note: If you are running R 4.1+ you can switch to the native pipe operator,
|>
. To do this to to Preferences > Code and check the box “Use native pipe operator”.Note also: An easy way to remember the Rename in scope shortcut is
Pipe + Alt
. To encapsulate text in quotes or brackets, highlight the text, then type
"
or(
.-
To generate code used to recreate an object:
x <- data.frame(a = 2, b = 4) y <- dput(x) #> structure(list(a = 2, b = 4), class = "data.frame", row.names = c(NA, #> -1L)) y #> a b #> 1 2 4
Writing loops
-
To generate a sequence of numbers to iterate over:
alternatively:
data <- data.frame(a = 1:3, b = 4:6) seq_len(nrow(data)) #> [1] 1 2 3
-
To print a counter to the console:
for (i in 1:3) { cat("\n", i) } #> #> 1 #> 2 #> 3
Note:
\n
moves the “print head” to a new line, whereas\r
will move it back to the start of the same line.
Command history
-
Keyboard shortcuts (from the console):
Description Mac Windows Cycle through previous commands Up arrow
Show previous commands Command + Up arrow
Auto-complete commands (from previous) Control + R
Writing functions
-
To generate a function skeleton as a code snippet, type
fun
then hit tab and return (to select the snippet):name <- function(variables) { }
Keep hitting tab to move from
name
tovariables
to thebody
of the function. -
Function extraction – to turn a chunk of code into a function, highlight a chunk of code, then press
Control + Alt + X
on Mac (or Windows) and enter a suitable function name when prompted. In doing so, this:x * y
turns into this:
multiply <- function(x, y) { x * y }
Note: You can make your own custom code snippets in Tools > Global Options > Code and clicking Edit Snippets in the Snippets section.
Graphics
-
To generate a sequence of evenly spaced values (useful when manually defining axis breaks in
ggplot2
): -
To list the names of built in colours:
Packages
-
Keyboard shortcuts:
Description Mac Windows Document package Command + Shift + D
Ctrl + Shift + D
Search
-
To select the path of a file in your current directory, hit Tab inside of:
""
-
To locate files relative to your project root, after installing the
here
package: -
Keyboard shortcuts:
Description Mac Windows Find files / functions Control + .
Ctrl + .
Search all files in a directory / project Command + Shift + F
Miscellaneous
-
Keyboard shortcuts:
Description Mac Windows Show command palette Command + Shift + P
Ctrl + Shift + P
Keyboard shortcut reference Alt + Shift + K
Zoom in on an RStudio panel Control + Shift + 1-4
To find RMarkdown cheat sheets in RStudio, go to Help > Cheat Sheets > R Markdown Reference Guide. You can also find cheat sheets for
dplyr
,ggplot2
, anddevtools
here.-
To automatically load packages on startup / restarting R:
- Create an
.Rprofile
file in your project root - Fill it with anything you want loaded on startup
Note that you should not use this to load in packages used in your analyses, such as
dplyr
orggplot2
.A useful example might be:
However, you can also populate it with any helper functions you might regularly use, or even R fortunes.
- Create an
-
To generate an R fortune, after installing
fortunes
:fortune()