Tips and tricks
December 20 2022
tips.RmdGeneral
-
Keyboard shortcuts:
Description Mac Windows Run lines of code Command + EnterCtrl + Enter(Un)comment lines of code Command + Shift + CCtrl + Shift + CInsert a foldable comments section Command + Shift + RCtrl + Shift + RReindent lines of code Command + ICtrl + IReformat lines of code Command + Shift + ACtrl + Shift + AAttempt code completion TabTabClear the console Control + LCtrl + LInsert an assignment operator, <-Alt + -Alt + -Insert a pipe operator, %>%Command + Shift + MCtrl + Shift + MRename a variable in scope Command + Shift + M + AltCtrl + Shift + M + AltNote: 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 #> 3Note:
\nmoves the “print head” to a new line, whereas\rwill 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 arrowShow previous commands Command + Up arrowAuto-complete commands (from previous) Control + R
Writing functions
-
To generate a function skeleton as a code snippet, type
funthen hit tab and return (to select the snippet):name <- function(variables) { }Keep hitting tab to move from
nametovariablesto thebodyof the function. -
Function extraction – to turn a chunk of code into a function, highlight a chunk of code, then press
Control + Alt + Xon Mac (or Windows) and enter a suitable function name when prompted. In doing so, this:x * yturns 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 + DCtrl + 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
herepackage: -
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 + PCtrl + Shift + PKeyboard shortcut reference Alt + Shift + KZoom 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, anddevtoolshere.-
To automatically load packages on startup / restarting R:
- Create an
.Rprofilefile 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
dplyrorggplot2.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()