RStudio addins, or how to make your coding life easier

[This article was first published on R on Stats and R, 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.

What are RStudio addins?

Although I have been using RStudio for several years, I only recently discovered RStudio addins. Since then, I am using these addins almost every time I use RStudio.

What are RStudio addins? RStudio addins are extensions which provide a simple mechanism for executing advanced R functions from within RStudio. In simpler words, when executing an addin (by clicking a button in the Addins menu), the corresponding code is executed without you having to write the code. If it is still not clear, remember that for importing a dataset in RStudio, you have two options:

  • import it by writing the code (thanks to the read.csv() function for instance)
  • or you can import it by clicking on the “Import Dataset” button in the Environment pane, set the importing settings, then click on “Import”

A RStudio addin is exactly like the Import Dataset button but for other common functionalities. So you could write code as you can import a dataset by writing code, but thanks to RStudio addins you can execute code without actually writing the necessary code. By using the RStudio addins, RStudio will run the required code for you. RStudio addins can be as simple as a function that inserts a commonly used snippet of code, and as complex as a Shiny application that accepts input from the user to draw a plot. RStudio addins have the advantage that they allow you to execute complex and advanced code much more easily than if you would have to write it yourself.

I believe addins are worth trying for all R users. Beginners will have the possibility to use functions that they would not have used otherwise because the code is too complex, whereas advanced users may find them useful to speed up the writing of their code in some circumstances. For other tips in R, see the article “Tips and tricks in RStudio and R Markdown”.

Installation

RStudio Addins are distributed as R packages. So before being able to use them, you need to install them. You can install an addin exactly the same way you install a package: install.packages("name_of_addin"). Once you have installed the R package that contains the addin, it will immediately become available within RStudio, via the Addins menu located at the top.

RStudio addins toolbar

Addins

If you are still not convinced, see below a list of the addins I find most useful together with concrete examples in the following sections.

Note that this list is not exhaustive and you are likely to find others useful too depending on what type of analyses you do on RStudio. Feel free to comment at the end of the article to let me know (and other readers) addins you found worth using.

Esquisse

{esquisse} is an addin developed by a French company called dreamRs. Here is how they define it:

This addin allows you to interactively explore your data by visualizing it with the ggplot2 package. It allows you to draw bar plots, curves, scatter plots, histograms, boxplot and sf objects, then export the graph or retrieve the code to reproduce the graph.

With this addin you can easily create beautiful graphs from the {ggplot2} package and the best part according to me is that you can retrieve the code to reproduce the graph. Compared to the default {graphics} package, it is true that graphs from the {ggplot2} package look usually better but the code is also longer and more complex. With this addin, you can draw graphs from the {ggplot2} package by dragging and dropping variables of interest in an user-friendly and interactive window, and then use the generated code in your script.

For the sake of illustration, let’s say wee want to create a scatter plot of the variables Sepal.Length and Petal.Length of the dataset iris and color the points by the variable Species. For this, follow these steps:

  1. load the dataset and rename it:1
dat <- iris
  1. install the package {esquisse}. This must be done only once
install.packages("esquisse")
  1. open the ‘ggplot2’ builder from the RStudio Addins menu:

Step 3: Open ‘ggplot2’ builder from the RStudio addins menu

  1. Select the dataset you want to work on (in this case dat) and click on “Validate imported data” after checking that the number of observations and variables are correct (green box):

Step 4: Select dataset and validate the imported data

  1. Drag and drop the variables of interest in the corresponding areas. In this case, we would like to draw a scatter plot of the variables Sepal.Length and Petal.Length and color points based on the variable Species:

Step 5: Drag and drop the variables in the corresponding areas

  1. Click on “ Export & code” at the bottom right of the window. You can either copy the code and paste it where you want to place it in your script, or you can click on “Insert code in script” to place the code where your cursor is located in your script:

Step 6: Retrieve the code to use it in your script

If you chose the second option, the code should appear where your cursor was located. Many different options and customizations are possible (e.g., axis labels, colors, legend position, theme, data filtering, etc.). For this, use the buttons located at the bottom of the window (“Labels & title”, “Plot options” and “Data”). You can see the changes instantly in the window, and when the plot corresponds to your needs, export the code into your script. I will not go into more details regarding the different types of plots and the customizations, but make sure to try other types of plots by moving variables and customize it to see what is possible.

Questionr

The {questionr} addin is useful for survey analysis and when dealing with factor variables. With this addin, you can easily reorder and recode factor variables. The addin also allows to easily transform a numeric variable into factors (i.e., categorize a continuous variable) thanks to the cut() function. Like any other addin, after having installed the {questionr} package, you should see it appearing in the addins menu at the top. From the addins dropdown menu, choose whether you want to reorder or recode your factor variable, or categorize your numeric variable.

Recoding factors

Instead of writing the recode() function from the {dplyr} package, we can use the {questionr} addin.

For this example, let’s say we want to recode the Species variable to shorten the length of the factors, and then store this new variable as Species_rec:

Step 1: Select the variable to be recoded and the recoding settings

Step 2: Specify the names of the new factors

Step 3: Check the results thanks to the contingency table and use the code at the top

Reordering factors

Similar to recoding, we can reorder factors thanks to the {questionr} addin. Let’s say we want to reorder the 3 factors of the Species variable such that the order is versicolor then virginica and finally setosa. This can be done as follows:

Step 1: Select the variable to reorder and the new variable name

Step 2: Specify the order you want

Step 3: Use the code at the top in your script

Categorize a numeric variable

The {questionr} addin also allows to transform a numeric variable into a categorical variable. This is often done for the age, when age is transformed into age groups for instance. For this example, let’s say we want to create 3 categories of the variable Sepal.Length:

Step 1: Choose the variable to be transformed and the new variable name

Step 2: Set the number of breaks equal to 3

(Try by yourself the other cutting methods and see the results directly in the window.)

Step 3: Check the result thanks to the barplot at the bottom

Step 4: Use the code in your script

Remedy

If you often write in R Markdown, the {remedy} addins will greatly facilitate your work. The addins allows you to add bold, creating lists, urls, italics, title (H1 to H6), footnote, etc. in an efficient way. I reckon that some of these tasks can be done faster by using the code directly rather than by going through the addins menu and then selecting the transformations you want. However, I personnaly cannot remember the code for all transformations, and it is quicker to apply it through the menu than by searching for the answer on Google or on your Markdown cheat sheet.

Styler

The {styler} addin allows to reformat your code in a more readable format by running styler::tidyverse_style(). It works both for R scripts and R Markdown documents. You can either reformat the selected code, the active file or the active package. I find this addin particularly useful before sharing or publishing my code, so that it respects the most common styling guidelines for code.

For instance, a piece of code like this:

1+1
#this is a comment
  for(i in 1:10){if(!i%%2){next}
print(i)
 }

becomes much more neat and readable:

1 + 1
# this is a comment
for (i in 1:10) {
  if (!i %% 2) {
    next
  }
  print(i)
}

Snakecaser

The {snakecaser} addins converts a character string to the snake case styling. Snake case styling is the practice of writing character strings of several words separated by space into character strings with words separated with an underscore (_). Moreover, it replaces capital letters with lowercases. For instance, the following string:

This is the Test 1

will be transformed to:

this_is_the_test_1

The snake case styling is particularly useful (and even recommended by many R users) for variables, functions and file names, etc.

ViewPipeSteps

Thanks to a reader of this article, I discovered the ViewPipeSteps addin. This addin allows to print or view the output of your pipe chain after each step.

For instance, here is a chain with the dataset diamonds:

library(tidyverse)

diamonds %>%
  select(carat, cut, color, clarity, price) %>%
  group_by(color) %>%
  summarise(n = n(), price = mean(price)) %>%
  arrange(desc(color))
## # A tibble: 7 x 3
##   color     n price
##   <ord> <int> <dbl>
## 1 J      2808 5324.
## 2 I      5422 5092.
## 3 H      8304 4487.
## 4 G     11292 3999.
## 5 F      9542 3725.
## 6 E      9797 3077.
## 7 D      6775 3170.

If you are unsure about your pipe chain or want to debug it, you can view the output after each step by highlighting your entire chain then clicking on “View Pipe Chain Steps” from the addins menu:

From the addins menu, you can choose to either print the result to the console, or view the result in a new pane (as if you called the function View() after each step of the pipe). Clicking on View Pipe Chain Steps will open a new window with the output of each step:

Note that you have to use the following command to install the ViewPipeSteps addin:

devtools::install_github("daranzolin/ViewPipeSteps")
library(ViewPipeSteps)

Now, you have no more excuses to use this pipe operator!

Ymlthis

ymlthis addin to easily write YAML header

The ymlthis addin makes it easy to write YAML front matter for R Markdown and related documents. The addin will create YAML for you and put it in a file, such as an .Rmd file, or on your clipboard.

This addin is particularly useful if you want to write (more complicated) YAML header.

Reprex

If you often ask the R community for help, this addin may be very useful!

It is important to remember that when you ask someone for help, you have to help them help you by giving a precise and clear overview of your problem. This helps the community to quickly understand your issue and thus reduces the response time.

In most cases, this involves providing a reproducible example, that is to say a piece of code (as small and as readable as possible) reproducing the problem encountered.

The {reprex} addin allows you to transform your reproducible example so that it can easily be shared on platforms such as GitHub, Stack Overflow, RStudio community, etc. The layout of your reproducible example will be adapted to the platform, and you can even include information about your R session.2

Here is how to use it steps by steps:

  1. First create your reproducible example in R (remember to keep it as short and readable as possible to save time to potential helpers):

Minimal reproducible example

  1. Select the {reprex} addin in the list of addins:

Select the {reprex} addin

  1. Select the platform on which you will post your issue (and check “Append session info” if you want to display your session info at the end of your reproducible example):

Set options in the {reprex} addin

  1. You can now see the output of your reproducible example (right panel), but more importantly, it has been copied in your clipboard and it is now ready to be pasted on the platform of your choice:

Your reprex is copied in your clipboard

  1. Paste your reprex on the platform of your choice (here, it is posted as an issue on GitHub):

Paste your reprex

  1. Check the final result of your reproducible example:

Final result

(Visit this GitHub issue to see the final result.)

Blogdown

I put this addin at the end of the list because it will be of interest for only a limited number of RStudio users: people maintaining a blog written in R like this one (see why I recommend everyone to start a technical blog).

The most useful functionalities in this addin are the following:

  • New post: create a new post with blogdown::new_post(). It can be used to create new pages as well, not only posts
  • Insert image: insert an external image into a blog post
  • Update metadata: update the title, author, date, categories and tags of the current blog post
  • Serve site: run blogdown::serve_site() to live preview your website locally

Thanks for reading. I hope that you will find these addins useful for your future R-related projects. See other tips and tricks in RStudio and R Markdown.

As always, if you have a question or a suggestion related to the topic covered in this article, please add it as a comment so other readers can benefit from the discussion.


  1. You actually do not need to rename it. However, I often rename the datasets I work on with the same generic name dat so when I reuse codes from a previous project for a new project, I do not have to change the name of the dataset in the code.↩︎

  2. Thanks to Josep for the suggestion.↩︎

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

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.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)