snippet packageslibrary(tidyverse)
library(scales)
library(Hmisc)
library(lme4)
library(arm)
R snippets
I’m always optimizing tasks to save time. Using keyboard shortcuts, for example, is probably the most basic way to speed up your tasks. Just imagine having to manually type every single pipe in R instead of simply using Cmd + Shift + M
, or using your mouse to click on run/render instead of using Cmd + Enter
/Cmd + K
. That would be something. In my post about \(\LaTeX\) macros, I provide some examples that help you automate your coding as you prepare articles and slides. You can do the same in RStudio for R, Markdown, or any other language you work with in RStudio. They’re called snippets, and they’re extremely useful.
Go to Tools > Global Options...
or simply press Cmd + ,
. Then click on Edit Snippets...
at the bottom. There, you will find a list of pre-existing snippets for R and other languages (including Markdown). Let’s take a look at some examples.
Example 1: loading packages
If you often use the same packages like I do, every single script starts with multiple library(...)
lines. Instead of manually entering your packages every single time, you can create a snippet that does that. Start a line with snippet
followed by a keyword you will later use to execute the snippet. Then, in a new line (notice the indentation), add the packages you’d like to load.
Once you save your snippet file, you’re ready to go. Now, when you start typing packages
, RStudio will suggest our new snippet. You can either press Enter
or Tab
to execute it and voilà: our list of packages is added to our script.
Example 2: longer functions
If there’s a function that you frequently use, a snippet can also be quite useful. For example, I use pivot_longer()
to transform my data on a regular basis. Considering its typical arguments, it’s much faster to simply type long
than to type the whole thing. So here’s a snippet I have that does just that.
snippet long= ${1:data} |>
long pivot_longer(names_to = ${2:names_var},
values_to = ${3:values_var},
cols = ${4:col}:${5:col})
Notice that I’m using #{n:X}
multiple times. This is a placeholder for arguments I’ll want to change as I use the function later on. The nice thing about using these placeholders is that we can press Tab
to navigate through the arguments, as shown below.
Example 3: sequences of operations
Finally, we frequently concatenate multiple operations in R. Let’s focus on two examples here. First, a simple one: suppose we want to transform all chr
columns into fct
. We’d normally type mutate(across(where(is_character), as_factor))
. If you do this a lot, why not turn it into a snippet?
snippet toFactormutate(across(where(is_character), as_factor))
Something I do all the time is calculate proportions after grouping variables. This will involve a sequence of operations and pipes. Here’s a simple snippet that helps us save time.
snippet prop${1:data} |>
summarize(n = n(), .by = c(${2:var_1}, ${3:var_2}, ${4:var_3})) |>
mutate(prop = n / sum(n), .by = c(${2:var_1}, ${3:var_2}))
To conclude, snippets can help you optimize your day-to-day tasks in RStudio. The examples above are all in R, but you can create snippets for all the available languages in RStudio (note that for Markdown, you will need to use Shift + Tab
to execute a snippet).
Copyright © 2024 Guilherme Duarte Garcia