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)
nameheightmasshair_colorskin_coloreye_colorbirth_yearsexgenderhomeworldspecies
Leia Organa15049brownlightbrown19femalefeminineAlderaanHuman
Obi-Wan Kenobi18277auburn, whitefairblue-gray57malemasculineStewjonHuman
Wilhuff Tarkin180NAauburn, greyfairblue64malemasculineEriaduHuman
Chewbacca228112brownunknownblue200malemasculineKashyyykWookiee
Han Solo18080brownfairbrown29malemasculineCorelliaHuman
Greedo17374NAgreenblack44malemasculineRodiaRodian

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

  1. Go to Tools in the RStudio menu bar
  2. Select Global Options
  3. Select Code in the left hand pane
  4. Select on the Edit Snippets button at the bottom of the window
  5. 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.

Previous
Next