Boost your R with RStudio addins

RStudio Addin 101 for beginners

Jinhwan Kim
6 min read1 day ago
Image from author, made with chatGPT

If you are using R for “data-things”, you probably use RStudio as IDE.

If you care about the R code you write pretty printing, you’ve probably used a styler before with Addins feature.

Image from r-lib/styler repository

In fact, the styler isn’t the only feature available as an addin. and these Addins create a huge productivity boost for coding in R for data science.

Image from author

What if you want to take advantage of your new features not available from existing R packages?

Today we’ll look at how to create this custom Addin.

Addins are defined as R functions that can be interacted with in the Rstudio IDE via keyboard shortcuts or the Addins menu.

While a normal R function takes input, runs an operation, and produces an output, addins have two different characteristics.

Image from author
  1. Addin is primarily intended to work with the Rstudio IDE, not with data.
  2. Because Addins are functions that can interact with the user, rather than interact with data, they provide a User Interface (UI)

For example. below R function adds 2 data i, j and return data. (and it may stored to “Environment”)

add <- function(i, j){
return(i + j)
}

And for addin example, styler’s style active file will read RStudio’s active file’s code and re-format it with defined code style. (not “Environment” change)

  • Addin vs Shiny

So, what’s difference between Addin and Shiny?

Shiny is way to provide functionality of R as web application. Which has many simillar characteristics between addin.

Personally, I think it’s better to use Shiny unless you need to interact with the RStudio IDE.

Developing RStudio Addin

For build RStudio Addin, you’ll need to be familiar with three packages. of course you should know about building R Package too.

  1. rstudioapi: Interact with RStudio IDE = SERVER for shiny
  2. miniUI: building UI for RStudio IDE = UI for shiny
  3. Shiny: Shiny !

So Let’s begin with make R package (name as “addin” for just now)

In this guide, I’d like to build simple function addRchunk . Which adds below text to utilize Rmarkdown or Quarto with simple keyboard shortcut

```{r} 


```
  • build R Function

First thing to do is make R code named as addRchunk and save it, clockAddin will be explained later, so don’t worry about it for now

# addRchunk.R
addRchunk <- function(){
rstudioapi::insertText("```{r}\n\n```\n")
}
Image from author

You should note 2 things in here.

  1. insertText function from rstudioapi package.
  2. separate text next line with \n

insertText is function that add given text to cursor’s location.

And there’s many functions in rstudioapi package, which can grouped as interact with Terminal, Session, Rstudio UI, Rstudio Project, Background jobs, Documents (code). You can see full material from here

After add addRchunk.R and build R package (using Cmd + Shift + B), You should be able to see the following run results

Image from author
  • binding keyboard shortcut

you’ve notice that, addRchunk adds as expected, but also not expected.

Because, we want to add r chunk in Source panel (code), but running addRchunk in console will add r chunk in Console.

So to solve this, we’ll bind keyboard shortcut to addRchunk and use it in Source panel (not console)

to bind keyboard, you should “transform” function to addin with dcf file. (*dcf file is just format for meta data.)

Image from author

Note that, dcf file must be 1. located in inst/rstudio, and 2. named as addins.dcf

Name: Insert r chunk
Description: Inserts `r code chunk` at the cursor position.
Binding: addRchunk
Interactive: false

and after Build package once again, you will be able to see it’s added as addin. (from RStudio menu: Tools > Addins > Browse Addins)

Image from author

Then we can bind shortcut using Keyboard Shortcuts as Cmd + R (after search with name defined in dcf file)

Image from author
Image from author

Shiny Gadgets: RStudio Addins with UI

From here, I’d like to introduce more advanced topic. Which is using the previously mentioned miniUI and shiny. And I’ll use example given with official document

So this type of addins are called as Shiny Gadgets.

Shiny Gadgets are basically Shiny Application. (but focused to interaction with RStudio) So it has UI and Server as common shiny application. with using miniUI package.

Let’s make R/clockAddin.R with below code.

library(shiny)
library(miniUI)

clockAddin <- function() {
ui <- miniPage(
gadgetTitleBar("Clock"),
miniContentPanel(
uiOutput("time")
)
)

server <- function(input, output, session) {
invalidatePeriodically <- reactiveTimer(intervalMs = 1000)
observe({
invalidatePeriodically()
time <- Sys.time()
output$time <- renderUI({
p(time)
})
})

observeEvent(input$done, {
timeText <- paste0("\"", as.character(Sys.time()), "\"")
rstudioapi::insertText(timeText)
stopApp()
})
}

viewer <- dialogViewer("Clock Addin")

runGadget(ui, server, viewer = viewer)
}

And update dcf file too.

Image from author
Name: Clock Addin
Description: show clock
Binding: clockAddin
Interactive: false

After Build Package and Run Addin (Clock Addin), you can similar result with right figure.

Image from author

What’s going on here?

The Shiny Gadget, which has UI like below image.

Image from author

will calculate Sys.time and update it by 1000Ms, and if user click Done, it will return time as text to cursor.

Image from author

runGadget, will work very simillar with shinyApp function of Shiny.

And viewer will define gadget’s type: paneViewer (Rstudio’s panel), dialogViewer (Independent Gadget UI), browserViewer (Web browser like chrome)

Summary

Let’s recap.

  • RStudio Addin, is R function which focus on Interaction between User and RStudio IDE. And it has two unique points: Keyboard binding or Shiny Gadget
  • You can easily build it just like common R Package or Shiny application (except adding dcf file)

And here’s personal tip.

  • If you want to well-made Rstudio Addin example, you would love to try esquisse.
  • If you need to help to build RStudio Addin, R package or Shiny application? Feel free to contact me. I’d like to listen your story.

Thanks !

--

--