RStudio Snippets for Markdown
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:
- The official RStudio tutorial on code snippets
- A Blog entry by Jake Daniels: How RStudio Snippets Improve My Productivity
- A Blog entry by Jozef Hajnala: 4 ways to be more efficient using RStudio’s Code Snippets, with 11 ready to use examples
- A Video by Sharon Machlis: R tip: Save time with RStudio code snippets (part of her video channel Do more with R)
In this post, I want to summarize some important details, which I learned laboriously after time-consuming trials:
- The Markdown section in the snippet editor works only with
.Rmarkdownfiles. – 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
- 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
- To insert snippets in the Markdown part of
.Rmarkdownfiles, you have to use SHIFT-TAB (and not just TAB as for R snippets in the code chunks).
- To write your snippets, you have to use the TAB key for indenting. 4 Spaces do not work!
- It is legitimate to write huge snippets for large code junks. For instance, you can write complex
ggplot2snippets as a kind of a template you are going to use frequently.
- For large code chunks, it may be preferable to write the snippets with the RStudio code pane. You can load the snippet files under
- You can share you snippets sending these files to your colleagues and collaborators.
- 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.