Closed Captioning Closed captioning available on our YouTube channel

R tip: Write your own RStudio addins

InfoWorld | Jan 28, 2019

This RStudio Addins tutorial shows step by step how to create an addin for the RStudio IDE

Similar
Hi. I’m Sharon Machlis at IDG Communications, here with Do More With R: Write Your Own RStudio Addins.
Writing RStudio addins is easy, fun, and takes just a few minutes! Well, that’s what Hao Zhu from the Marcus Institute for Aging Research told RStudio Conference. It hadn’t even occurred to me to try to write one, even though I use them pretty often. So, let’s take a look at how to make one.
What can you do with an addin? Here’s a slide from RStudio’s Jonathan McPherson, from back when
So, an addin can help you write code, format code, integrate with external data, kick off scripts … as McPherson said, pretty much anything that regular R code can do.
A couple of things we need to know about addins before we get started:
They’re regular R code. More specifically, an addin is primarily an R function. And that function has to use the rstudioapi package somehow – that’s how the addin interacts with the RStudio application.
Addins need to be part of an R package. They can’t be a stand-alone function. They need a package structure around them.
And, addins need to include a .dcf file with information about each addin in a package – metadata in a specific format.
Here’s the surprisingly easy-to-write addin function that Hao Zhu demo’d at RStudio Conference:
I’ll walk you through how to build another addin: an addin to edit an R snippets file. That file is in this location here: my home directory, then folder .R, then subfolder snippets, and the file r. snippets. I can open that file for editing with the usethis package’s edit_rstudio_snippets() function; or, by going several layers deep in the RStudio menu. But let’s see how to make an addin to do the same thing.
First, I need an R package. If you look at the Addins menu, you can see the snippets are arranged in alphabetical order by the name of their package. I want mine to be first, so I named my package asnippet. You can set up a new package with most of the directory structure you need by going to File > New Project, choose new directory, and then choosing Package.
New Project > New Directory > Choose Package> I already did that, and I’ll switch to my asnippet folder and project.
Here’s how I could open my snippets file with R code. I set the location of the snippets file in a variable, then use base R’s file.edit() function to open it for editing.
But, remember: A snippet has to interact with the RStudio API, using the rstudioapi package inside a function. So I can’t use just this code for a snippet. Instead, I’ll write a function that sends the file.edit() command to the RStudio console, using rstudioapi’s callFun() helper function.
This is a multi-step way to do a simple thing, but bear with me here. callFun() calls an RStudio function. The function we want to call, is “sendToConsole.” That just means we’re sending code to the console. The second argument is what we want to send to the console – in this case the file.edit() code that will open my snippets file.
The last piece for a working addin is a separate .dcf file, addins.dcf. That is a plain text file, and it needs to live in a folder called rstudio under a folder called inst. Those folders aren’t created by default when you set up a package, so you’ll need to create them manually. I did that in advance, you can see that here.
The addins.dcf file needs 4 lines of text for each addin: Name, Description, Binding, and whether it’s interactive. Name is what you want to call the addin. Description is fairly obvious. Binding is the name of your function for the addin. And interactive is either true or false: RStudio needs to know whether the addin is going to be waiting for user input. In this case, the answer is false.
You can see my addin name is Edit snippets, the description is Opens RStudio snippets file for editing, the Binding – name of the function – is edit_snippets, and it’s not interactive. That’s it. Now I just need to build and install the package. I can do that from the RStudio Build tab, I’ll click on the install and restart
Now if I look at my Addins dropdown, I should see Edit snippets under Asnippet. And if I click on the Edit snippets addin, my snippet file should pop open. And there’s my file.
Why do an addin instead of just running the function manually? One advantage of addins is that you can assign a keyboard shortcut to them. So if I go to Tools > Modify Keyboard Shortcuts Modify Keyboard Shortcuts> I can assign a keyboard shortcut to my edit snippets addin. I’ll search for snippets, then click next to the name and type shift-alt-cmd-S. < You can’t see me type, but now if I type shift-alt-cmd-S, my snippet file pops open for editing.
Looks like Hao Zhu was right: fast, simple, and fun!
That’s it for this episode, thanks for watching! For more R tips, head to the Do More With R page at https go dot infoworld dot com slash more with R, all lowercase except for the R. You can also find the Do More With R playlist on YouTube. Hope to see you next episode!
Popular
Featured videos from IDG.tv