RStudio Addin
How to write your own Rstudio Addin
If you want to create your own RStudio addins, all you need to do is:
- Create an R package
- Create some R functions
- Create a file at
inst/rstudio/addins.dcf
Links
- https://rstudio.github.io/rstudioaddins/
- https://github.com/rstudio/addinexamples
devtools::install_github("rstudio/addinexamples")
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:
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.
"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:
Create keyboard shortcut
In Tools > Addins > Browse Addins
we can set a shortcut for the addin.
In our case SHIFT + STRG + ALT + s
.