{blogsnip}: an RStudio addin package

The blogsnip addin is demonstrated by selecting 'insert details block' from the addins menu, which creates an expandable details section when rendered from R Markdown to HTML.

Inserting a details block with the {blogsnip} addin

tl;dr

On my commute home I made {blogsnip}: a tiny package of RStudio addins that add snippets of R code to help me write blog posts.

Install with:

install.packages("remotes")
remotes::install_github("matt-dray/blogsnip")

The problem

I’m lazy.

There’s some bits of code I often want to put in my blog posts (written in R Markdown) but I can’t remember them.

In particular:

Originally I stored them in a GitHub Gist, but this is sub-optimal: the internet connection on my commute can be patchy. I needed an offline solution.

The answer

I use RStudio and {blogdown} to write my blog posts. RStudio has a feature that lets you execute functions from an ‘addins’ menu.

Lots of of addins are available (see this RStudio Community thread). Some simple ones are {datapasta} for copy-pasting stuff into R, {reprex} for generating reproducible examples and even {blogdown} itself, which has addins for creating new posts and inserting images, for example.

So I wrote my snippets into a package that, when installed, adds some functions to RStudio’s addins menu.

The package is called {blogsnip}. Visit the site or source. Install with:

install.packages("remotes")
remotes::install_github("matt-dray/blogsnip")

Once installed, the functions are available under ‘BLOGSNIP’ in RStudio’s ‘addins’ dropdown (see the gif at the top of this post). You could also use them like regular functions by calling blogsnip::addin_details(), for example.

How to

You can follow RStudio’s guidance on creating addins, which even incldues some fancy stuff like invoking Shiny-powered interfaces for addins.

But perhaps, like me, you just want to create some simple shareable addins that insert some snippets of text. In which case, the (simplified) steps are:

  1. Create an R package structure with usethis::create_package()
  2. Add an R script to house your functions with usethis::use_r()
  3. Edit the script so you have functions in the form fun_name <- function() {rstudioapi::insertText("Text")}, where the insertText() function does exactly that (see example)
  4. Add a special file at inst/rstudio/addins.dcf that declares your functions as addins (see example)
  5. Document and build your package and host it for others to use

So you’re writing an R package as usual2, making use of {rstudioapi} functions and adding that special .dcf file.

See what this looks like in the full source code for {blogsnip} (note that I’ve added some other stuff there, like a license, readme and files to generate a {pkgdown} site).