RStudio Addin

RStudio Addin

How to write your own Rstudio Addin

If you want to create your own RStudio addins, all you need to do is:

  1. Create an R package
  2. Create some R functions
  3. Create a file at inst/rstudio/addins.dcf

1. Create am R Package

Set up tools for package development

library(devtools)
library(roxygen2)

# getwd() 
# setwd("path/to/repo")

Create Package

I am mainly following: https://hilaryparker.com/2014/04/29/writing-an-r-package-from-scratch/

create("rstudio_addin")

This creates the following files and folder structure:

RStudio Folder Structure

2. Create some R functions

We will put the following code in our package, more specifically into the R folder:

serverlessAddin <- function() {
  rstudioapi::insertText(" Rstudio Addin works ")
}

Document

document()

Use STRG + SHIFT + d for document.

Build & Install

Use STRG + SHIFT + b for build.

library(rstudio_addin)

3. Register the Addin

Create addins.dcf

The last step is to create a file at inst/rstudio/addins.dcf within your R package.

https://github.com/rstudio/webinars/blob/master/17-Understanding-addins/Understanding%20RStudio%20Add-ins.pdf

"C:\Users\u1341vs\Documents\R\win-library\3.4\addinexamples\rstudio"
"C:\Users\u1341vs\Documents\R\R-3.5.3\library\addinexamples

This is what the content of the file looks like:

Name: Find and Replace
Description: Find and replace words in a document.
Binding: findAndReplaceAddin
Interactive: true

Name: Reformat R Code
Description: Reformat R code using 'formatR::tidy_source()'.
Binding: reformatAddin
Interactive: true

Name: Subset a Data Frame
Description: Interactively subset a data frame.
Binding: subsetAddin
Interactive: true

Therefore we simply add the following lines of code in the mandatory format:

Name: RStudio Addin
Description: Creates a DOCKERFILE with the current content.
Binding: rstudio_addin
Interactive: false

After installing the package and restarting RStudio we have a new Addin:

addin menu

Create keyboard shortcut

In Tools > Addins > Browse Addins we can set a shortcut for the addin.

In our case SHIFT + STRG + ALT + s.