Innovation R

How RStudio Snippets Improve My Productivity

How to Create RStudio Snippets for R, Python, JavaScript, HTML, CSS and C++

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.

dty5fiyuwaayl1j (1)

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…


…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:

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

# 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")

# 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:

  1. Make sure the word “snippet” is blue
  2. Use Tab on every line of code below your snippet
  3. 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.

3 comments on “How RStudio Snippets Improve My Productivity

  1. very useful thanks for writing this up !

    Liked by 1 person

  2. Marcelo Carvalho dos Anjos

    Absolutely awesome and userful feature. Thanks for sharing this.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: