Site icon R-bloggers

Creating an RStudio Addin

[This article was first published on stevenmortimer.com, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I never thought too much about RStudio addins, but I saw a few cool examples ( jadd, colorpicker, and addinslist) and decided to take a closer look. After a little research, it seemed easier than I thought. It was easier because RStudio provided some excellent documentation on addins and it turns out that if you can write R code, then you can write an RStudio addin.

My Idea

Around the time I was looking at addins, I started taking advantage of a little RStudio trick to navigate long scripts. If you create comments that are a pound sign and end with at least 4 dashes (# Comment ----), then they will automatically be organized and appear in an organizational perspective at the bottom left of the Source pane.

I’ll take any extra organizational help I can get so I started annotating my code with comments like these to break it up. Then the idea hit me to create an Addin and link a keyboard shortcut to it so I can format comments quickly to this standard.

The Implementation

The logic is written in R code and the rstudioapi package is used so that your code can interact with RStudio. The R code can be complex or really simple (see the RStudio example on Addin basics). However, you must structure your project with certain metadata, like an R package, that will allow RStudio to automatically discover and register these addins when the addin is installed.

The R code should be documented using the roxygen2 style. Here is the small bit of regex that powers most of my sectioncomment addin.

#' Function to create the padded comment
#' 
#' This function is the logic that runs to reformat
#' the comment as a section break
#' 
#' @usage comment_styler(x, doc_mode=FALSE, l = 80)
#' @importFrom stringr str_pad
#' @param x a string to format
#' @param doc_mode a logical indicating only to convert 
#' lines starting with at least a double hash sign
#' @param l an integer indicating length to pad
#' @return a string that is formatted
comment_styler <- function(x, doc_mode=FALSE, l=80) {
  
  starter_regex <- '(\\s*#+\\s*)(.*)'
  
  if(doc_mode)
    starter_regex <- '(\\s*#{2,}\\s*)(.*)'
  
  if(grepl(starter_regex, x)){
    clean_x <- trimws(gsub('-*$', '', gsub('-+\\s+-+', '', trimws(x))))
    x <- str_pad(gsub(starter_regex, '# \\2 ', clean_x), 
                 width=l, side='right', pad='-')  
  }
  
  return(x)
}

As you can see, it’s not very sophisticated. I just look for text in the Source pane that appear to be a section comment and pad the line up.

How to Use this Addin

If you like the idea of my Addin you can install from RStudio with the following command:

devtools::install_github("StevenMMortimer/sectioncomment")

After installing it should apper in your Addins dropdown and you can use it, like so:

My favorite reason to use is that I can write an entire outline of the steps I want to perform before writing a single line of code, then turn those steps into sections.

This a great way to force yourself to think about what you are going to code before diving in head first and getting lost in a jumbled mess of your own thoughts. I hope you enjoy using it as much as I do and you consider creating your own addin.

To leave a comment for the author, please follow the link and comment on their blog: stevenmortimer.com.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.