Calli Gross My personal Blog

ggThemeAssist an add-in for RStudio

What is an add-in?

With the release of RStudio v0.99.878 support for registering custom RStudio Addins has been added to the long list of RStudio features.

Add-ins are

RStudio Addins provide a mechanism for executing custom R functions interactively from within the RStudio IDE—either through keyboard shortcuts, or through the Addins menu. Coupled with the rstudioapi package, users can now write R code to interact with and modify the contents of documents open in RStudio.

ggThemeAssist is such an add-in which tries to help the user to alter ggplot2 themes in a WYSIWYG way. This enables the user to change the plot with instant preview and relieves him from manual trial & error procedures. ggThemeAssist can be found on github.

Screenshot of ggThemeAssist

When the done button is clicked, a string is returned, which sets all theme elements to the corresponding values:

gg + theme(axis.title = element_text(size = 20), 
    axis.text = element_text(size = 14), 
    legend.title = element_text(size = 15), 
    panel.background = element_rect(fill = "azure2"), 
    legend.position = "bottom", legend.direction = "horizontal") + 
    labs(colour = "Cylinder")

Motivation

When I read about the new add-in feature in the RStudio blog the first time, I thought this might be a great thing for the RStudio IDE, maybe not as big as the App Store for Apple, but a great way to enhance the IDE with great ideas from the vital R community. I hope the awesome guys from RStudio will add a catalog to their website to make it easier to find add-ins the community build.

Usually I’m a lazy guy and I can’t remember all the different themes for ggplot2, so most of the time I just added theme_bw() and accepted the outcome. From time to time this was not enough, so I was fiddled around a plot and thought:

There needs to be an add-in!

Unfortunately there was none, so I started to read through the great documentation how to build an add-in and started playing around. I won’t forget how I posted an early screenshot on twitter and @0ttlngr asked me if I would share my code. It was my intention from the beginning on to use github, which was totally new for me. After my initial commit it took only a couple of hours and I had my first pull request. It felt awesome. Since this initial commit on Feb. 20, 2016 ggThemeAssist collected 95 stars and every single one was a new motivation for me to keep going, despite having a full time job which demands me 60-80 hours/week.

How it works

Installation

If you want to give ggThemeAssist a try, you can install from CRAN or use the latest version from github. I recommend the latter, as due to the CRAN policy submissions should be done only every 1-2 months. As ggThemeAssist is under heavy development and I’m a less experienced developer, updates happen on a much more regular basis than that.

Please ensure you are using at least v0.99.878 of RStudio. Otherwise download it from their website.

To install the latest stable version from github just copy & paste the following code into your R session:

if (!requireNamespace("devtools", quietly = TRUE))
  install.packages("devtools")

devtools::install_github("calligross/ggthemeassist")

or use the version from CRAN (not recommended):

install.packages('ggThemeAssist')

After the installation ggThemeAssist should show up in your Addins menu: Addin menu

Usage

There were two and thanks to Dean Attali there are three possibilities to use ggThemeAssist:

  1. Highlight an ggplot2 object in the editor and start the add-in Option 1
  2. Highlight the code for a ggplot2 plot and start the add-in Option 2
  3. Start ggThemeAssist from the console: ggThemeAssist::ggThemeAssistGadget(gg)

Features

At present not all themes can be edited with ggThemeAssist and I’m not sure if it will ever happen. There are endless possibilities and in my opinion the UI already needs a complete overhaul (see below). But at the moment you are able to change the following elements:

  • Plot and panel background
  • panel.grid.major and minor
  • axis.title.x and y
  • axis.text
  • axis.ticks
  • axis.line
  • legend.background
  • legend.key
  • legend.text
  • legend.title
  • legend.position (just click in the plot)
  • legend.direction
  • Labels (title, subtitle, caption, legends)

When placing the legend inside the plot, don’t forget to set the dimensions in the settings tab to corresponding values. Otherwise the legend won’t be placed as desired.

Misstakes

Well, I’m not a developer (actually I’m a financial auditor) and this was my first (public available) package and contact with miniUI. I hope my mistakes help other to get around these pitfalls.

Test your UI on different screen resoultions!

I’m developing on a 27” iMac with a 5k screen. Everything looked quite nice, but it didn’t take long until an issue was opened on github because the UI was only hard to use on smaller screens.

Fixed size

As you can see, there were only two rows of input widgets which was not intended.

Use relative width/height when building a UI in shiny!

plotOutput("ThePlot2", width = '100%', height = '45%')

Backward looking I can`t remember why I used a fixed size, but I guess I had several problems with correct rendering and fixed one of them with using a fixed height.

Don’t submit to CRAN too early

The UI issue was opened several hours after the CRAN release. Due do the CRAN release policy and my respect for the volunteers working there, I didn’t yet submit a bug-fix release to CRAN. So my advice:

Test as much as you can before submitting to CRAN and most important: Let others test your gadget/add-in as well!

Look at other add-in issues to prevent their mistakes

Read through the issues other developer of add-in or shiny widgets had. Learn from them. You might find most mistakes I made here.

Outlook

Optional multiline style results

ggThemeAssist returns at present a one-liner which is mangled through formatR:

gg + theme(axis.title = element_text(size = 20), 
    axis.text = element_text(size = 14), 
    legend.title = element_text(size = 15), 
    panel.background = element_rect(fill = "azure2"), 
    legend.position = "bottom", legend.direction = "horizontal") + 
    labs(colour = "Cylinder")

But many users don’t write their code in this way and I personally prefer to build my plot over several lines like this:

gg <- gg + theme(axis.title = element_text(size = 20))
gg <- gg + theme(axis.text = element_text(size = 14)) 
gg <- gg + theme(legend.title = element_text(size = 15)) 
gg <- gg + theme(panel.background = element_rect(fill = "azure2"))
gg <- gg + theme(legend.position = "bottom", 
                 legend.direction = "horizontal")
gg <- gg + labs(colour = "Cylinder")

UI overhaul

As more and more features had been added to ggThemeAssist, the UI didn’t evolve as well. A complete overhaul of the UI will be the next (big) thing to do, but except that it should reflect the workflow of as many ggplot2 users as possible, I’m yet unsure how it should be like. If you have an idea, please discuss with us on github or leave a comment below.

ggThemeAssist is made for the community of R users and is my personal give back to all you great guys out there!

Others?

Please open an issue or even better submit a pull request if you have any other ideas! In case you are willing to contribute, please use a dev/feature branch for new features and the master branch only for bug-fixes.

Thanks

I would like to thank to all who opened issues on github and helped to improve ggThemeAssist.

Especially I would like to thank ottlngr and daattali for their contributions and the discussions we had.