R Snippets
One of my favorite features in RStudio is the code snippet. This feature allows users to save frequently used templates of code that can be recycled for other uses. For example, if there’s a function or chain of piped commands that one uses frequently, instead of writing these from scratch every time, the code snippet can be used to insert the template that can then be filled in with . Some snippets are already baked into RStudio, but you can also create your own for your own specific needs.
Examples of snippets
Not in function
I frequently use the dplyr %in%
operator to filter rows from one data frame by a vector in another data frame. However, sometimes it’s nice to be able to do the opposite and filter a dataframe by values that are not in a vector. An example may be when I need to find a set of participant IDs that are not in another list. One solution that has been proposed for this task is to negate the %in% operator with a function.
library(dplyr)
`%!in%` = Negate(`%in%`)
While this certainly gets the job done, I never liked having to declare this function before its use. Rather than creating a negate function, one could negate %in%
in a call to the filter verb. Below is the general approach where the exclamation point is placed before the column to be filtered. I would read this as “filter rows where the homeworld is not in worlds_of_interest”.
worlds_of_interest = c("Tatooine", "Naboo")
# Filter rows that do not match worlds_of_interest
filter(starwars, !homeworld %in% worlds_of_interest)
name | height | mass | hair_color | skin_color | eye_color | birth_year | sex | gender | homeworld | species |
---|---|---|---|---|---|---|---|---|---|---|
Leia Organa | 150 | 49 | brown | light | brown | 19 | female | feminine | Alderaan | Human |
Obi-Wan Kenobi | 182 | 77 | auburn, white | fair | blue-gray | 57 | male | masculine | Stewjon | Human |
Wilhuff Tarkin | 180 | NA | auburn, grey | fair | blue | 64 | male | masculine | Eriadu | Human |
Chewbacca | 228 | 112 | brown | unknown | blue | 200 | male | masculine | Kashyyyk | Wookiee |
Han Solo | 180 | 80 | brown | fair | brown | 29 | male | masculine | Corellia | Human |
Greedo | 173 | 74 | NA | green | black | 44 | male | masculine | Rodia | Rodian |
An alternative approach where the data frame is piped into the filter verb will produce the same results.
# Filter rows that do not match via pipe operator
starwars %>%
filter(!homeworld %in% worlds_of_interest)
How to create your own snippets
- Go to Tools in the RStudio menu bar
- Select Global Options
- Select Code in the left hand pane
- Select on the Edit Snippets button at the bottom of the window
- The next windows should display the R code snippets.
# my snippets ---------------------------------------
snippet notin
df %>% filter(!${1:col_name} %in% ${2:col_name2})
In my code snippet, beginning to type "notin"
in RStudio will present auto complete options. If I press tab to autocomplete the snippet, it will complete the function where the function has been typed out and all that is needed is to fill in the place-holders, "df"
, "col_name"
, and "col_name2"
. In the snippets file, each variable that will need to be filled in is encased in curly braces preceding with a dollar sign, and each variable is also preceded with a number and a colon. The numbers are the order of variables. I saved this snipped towards the top of my snippets file within a section called “my snippets.”
I hope this gets the point across of how snippets can be useful to save you time while coding. They’re especially useful for code chunks that are often reused or for functions that you want to avoid looking up when you need them.