Do you find yourself writing the same code day after day?
Are you opening other projects to copy the code so often you think: there must be an easier way?
Well, creating a snippet in RStudio may help resolve your frustrations.
Convenience At Your Fingertips
Snippets are commands you run in RStudio that load pre-built lines of code. They are often utilized to shorten things you frequently type – sort of like a big clipboard.
There’s no ultimate snippet guide. But creating your own snippets make your job easier specific to how you operate.
For example, one thing that always frustrates me is when I make a typo writing packages. My brain starts to sizzle if I spend more than a minute doing so. Snippets resolve this frustration. And I can’t stress enough how convenient it is in my day-to-day operations.
So when I type libraries…
library(tidyverse)
library(lubridate)
library(ggthemes)
library(tidytext)
library(caret)
…that command is actually a snippet that contains all my common packages. I named it libraries so when I begin to write the usual library() function, I see the snippet in the drop-down list of suggestions and I can load it all in – no more typos!
David Hood shares on Twitter that he names a snippet “usual_suspects” to load his common libraries:
Because I use them a lot in #rstats, in Rstudio – Global Options – Code – Editing – Snippets, I have a usual_suspects snippet which adds all my common libraries. Type usu, do the tab complete for usual_suspects, and get all the code pic.twitter.com/fD1reNQQby
— David Hood (@Thoughtfulnz) December 2, 2018
It’s wildly convenient. So convenient it takes four-clicks and you’re done loading your libraries. Are you starting to realize how these quality-of-life snippets can lead to better productivity?
Another example could involve pre-coding common steps in a machine-learning workflow – like set.seed, split data, and the logistic regression formula with basic arguments. You could save it as such: logistic_regression_example
# train a model and summarize model library(caret) set.seed(2019) # data split 75% train - 25% validate index_train <- sample(1:nrow(dataset), 3/4 * nrow(dataset)) training_set <- dataset[index_train, ] test_set <- dataset[-index_train, ] # training the model fit.glm <- glm(y ~ ., data = training_set, family = "binomial") summary(fit.glm) # predict on test set fit.glm_pred <- predict(fit.glm, newdata = test_set, type = "response") # constructing a confusion matrix (fit.glm_conf_matrix_50 <- table(test_set$y, fit.glm_pred) %>% confusionMatrix())
Your snippet loads all the lines of code – all you need to do is fill in the blanks. It helps beginners who need the framework, but also for senior data analysts who’ve refined their workflows and just want a quick command for loading it all at once.
Some other examples I build snippets for:
- Company-specific colour-wheels I need
- Vectors with all the variable names in my datasets
- Specific graph archetypes I frequently use
You’ll start to realize how much of your daily coding could benefit from a snippet.
How to Create Snippets in RStudio
Do you see the potential boosts in productivity you can gain from a well-made snippet? Here’s how to start making yours:
1. Open Global Options in RStudio
2. Go to the Code Tab and Edit Snippets (make sure it’s enabled).
3. Choose your language and begin writing.
Note: making sure snippets are correctly saved is important. They can be finicky. Some quick things to troubleshoot:
- Make sure the word “snippet” is blue
- Use Tab on every line of code below your snippet
- Look at the drop down arrows beside the line-numbers – make sure when you open and close them the code collapses correctly
Begin Increasing Your Productivity
Snippets enhance your current workflows. They are simple to add and forego the need to maintain a text document with rows of uncategorized code.
I hope this quick introduction to snippets will help identify when you should create one and lets you improve your own quality-of-life as a programmer.
Hey, if you use snippets already and want to share their creative or practical uses, leave a comment or tag us in a post – we’d love to help share it! Here’s DataCritics’ Twitter and LinkedIn pages.
very useful thanks for writing this up !
LikeLiked by 1 person
Thank you for the kind words Mandip. Something simple for all levels of programmers to use.
LikeLike
Absolutely awesome and userful feature. Thanks for sharing this.
LikeLike