3 min read

dater - a tiny Addin for RStudio

Inserting a date with a keyboard shortcut

TLDR: You can find the dater package on github.


Update 1: It seems all of this was for nothing…

…but still, it’s a good excuse to learn more about Addins!


Update 2: Jose (see above) was kind enough to expand on his original response to my tweet about this post and has introduced me to the wonderful world of RStudio snippets! Definitely check them out if you’re interested in automatic text expansion and magically inserting code snippets when using RStudio.


I stopped using Windows as my daily driver many years ago and have been lucky enough to be able to use either a Mac or Linux desktop for the vast majority of my work.

Despite not missing Windows at all, there was one thing I used a lot in my note-taking that I did miss. Windows ‘notepad’ has a feature where when you press the ‘F5’ key, it inserts the date and time into the current document.

I used this a lot to help me organise my text based notes and I’ve always missed this in other applications. Since I switched most of my (computer based) note taking to Rmarkdown a while back, I’ve thought that it would be nice to bring the ‘insert current date’ feature to RStudio. So today I did just that.

Luckily for me, RStudio has a really simple plugin system called ‘Addins’ and we can use those to add additional features to the IDE.

Addins are really simple to create, you just need a package with a special file in it that defines a function from that package as an Addin, and once installed, your function is then available from the Addins menu.

To create my ‘insert date’ Addin, I first needed to create a new package. To do this I created a new project in RStudio and chose the option to create a package. Next I removed the file from the man directory and the hello.R file from the R directory.

Now we need to add our insert date function.

insertDate <- function(){
  rstudioapi::insertText(paste0("# ", as.character(Sys.Date()), "\n"))
}

The insertDate function here uses there rstudioapi package to insert text at the current cursor location, this goes in a new file in the R directory.

Next we need to tell RStudio that this function should be used as an Addin. In the ‘inst’ directory, create a folder called ‘rstudio’ and in that, create a file called ‘addins.dcf’. This needs to look something like the following:

Name: Insert date
Description: Inserts the current date at the cursor position.
Binding: insertDate
Interactive: false

Once that’s done, we can edit the DESCRIPTION file with some proper information about our new package and then test is with the ‘check’ and ‘Install and Restart’ buttons on the ‘Build’ tab.

Once restarted you should be able to see your function listed in the Addins menu. And you’re done! I realise I’ve taken some shortcuts with the package, but it’s never going on CRAN and it’s mainly for my own use and for demo purposes, so I’m not too bothered at this point.

If you want to go a step further you can also assign a keyboard shortcut to your Addin function as described in the documentation.

Obviously I’ve just described a really simple use case here, but Addins can do anything that R can do. Then, by using the rstudioapi package, you can interact directly with RStudio itself, so there are loads of opportunities to get creative and make Addins that do really cool things.

If you want to check out the package I made, you’ll find it on github, here.