RStudio Snippets for Markdown

Tutorial for RStudio code snippets

Code snippets are text macros which you can insert into your programs. They are used for repetitive tasks and can boost your productivity tremendously. Here I am not going to explain how to use it. There are already several good tutorials around:

Eight useful reminders

In this post, I want to summarize some important details, which I learned laboriously after time-consuming trials:

  1. The Markdown section in the snippet editor works only with .Rmd and .Rmarkdown files. – So if you are going to write just text (e.g., a blog entry) and you want use RStudio snippets to insert shortcodes than you have to write into .Rmd / .Rmarkdown files.
  2. The snippets in the R section of the snippet editor are for code chunks whereas the snippets in the Markdown section work only in markdown text of .Rmd / .Rmarkdown files.
  3. To insert snippets in the Markdown part of .Rmd / .Rmarkdown files, you have to use SHIFT-TAB (and not just TAB as for R snippets in the code chunks).
  4. To write your snippets, you have to use the TAB key for indenting. 4 Spaces do not work!
  5. It is legitimate to write huge snippets for large code junks. For instance, you can write complex ggplot2 snippets as a kind of a template you are going to use frequently.
  6. For large code chunks, it may be preferable to write the snippets with the RStudio code pane. You can load the snippet files under ~/.R/r.snippets respectively ~.R/markdown.snippets into RStudio.
  7. You can share you snippets sending these files to your colleagues and collaborators.
  8. Do not use names, which are difficult to type to call your snippets. For instance, to insert the markdown syntax for an image do not name it “![” but “ii” for example.

Edit this page

Peter Baumgartner
Peter Baumgartner
Retired Professor of Technology Enhanced Learning (TEL)

My research interests include eLearning, educational technology, educational design, open science and data science education.