by Sebastian Wolf

To give you an idea of the scale I am talking about, an automotive metaphor might be useful. A typical Shiny app I see in my daily work has about 50 or even fewer interaction items. Let’s imagine this as a car. With less than 50 interactions, think of a small car like a Mini Cooper. Compared to these applications, with more than 500 interactions, bioWARP is a truck, maybe even a “monster” truck. So why do my customers want to drive trucks when everyone else is driving cars?

I concluded that most people just don’t need to build them that big! So now, I would like to explain why we needed such a large app and how we went about building it.

Last month, at the R/Pharma conference that took place on the Harvard Campus, I presented bioWARP, a large Shiny application containing more than 500,000 lines of code. Although several other Shiny apps were presented at the conference, I noticed that none of them came close to being as big as bioWARP. And I asked myself, why?

Additionally, it was required to write the whole application in R , as all our mathematical packages are written in R . So we decided to do it all with shiny because it already covers two of the three main user requirements: being pretty and being interactive.

Building software often starts with checking the user requirements. So when we started the development of our statistical web application, we did that, too. Asking a lot of people inside our department, we noticed that the list of requirements was huge:

How did we build the truck?

Modularity + Standardization Inside our department, we were running some large-scale desktop applications already. When it came to testing, we always noticed that testing takes forever. If one single piece of software gathers data, calculates statistics, provides plot outputs, and renders PDF reports, this is a huge truck and you can just test it by driving it a thousand miles and see if it still works. The idea we came up with was building our truck out of Lego bricks. Each Lego brick can be tested on its own. If a Lego wheel runs, the truck will run. The wheel holder part is universal, and if we change the size of the wheels, we can still run the truck, assuming each wheel was tested. This is called modularity. There exist different solutions in R and shiny that can be combined to make things modular: Shiny Modules Object orientation R-packages Clever name-spacing As Shiny modules did not exist when we started, we chose options 2 and 3. As an example, I’ll compare two simple Shiny apps representing two cars here. One is written using object orientation; one is a simple Shiny application. The image below illustrates that the renderPlot function in a standard Shiny app includes a plot, in this case using the hist function. So whenever you add a new plot, its function has to be called inside. In the object-oriented app, the renderPlot function calls the shinyElement method of a generic plot object we created called AnyPlot . The first advantage is that the plot can easily be exchanged. (Please look into the code if you wonder if this really is so.) To describe that advantage, you can imagine a normal car, built of car parts. Our car is really a a Lego car, using even smaller standardized parts (Lego bricks) to construct each part of the car. So instead of the grille made of one piece of steel, we constructed it of many little grey Lego bricks. Changing the grille for an update of the car does not require us to reconstruct the whole front; just use green bricks instead of grey bricks, for example, since they should have the same shape. By going into the code of the two applications, you see there is a straight-forward disadvantage of object orientation: there is much more code. We have to define what a Lego brick is and what features it shall have. Object oriented shiny app library(methods) library(rlang) setGeneric("plotElement",where = parent.frame(),def = function(object){standardGeneric("plotElement")}) setGeneric("shinyElement",where = parent.frame(),def = function(object){standardGeneric("shinyElement")}) setClass("AnyPlot", representation(plot_element = "call")) setClass("HistPlot", representation(color="character",obs="numeric"), contains = "AnyPlot") AnyPlot <- function(plot_element=expr(plot(1,1))){ new("AnyPlot", plot_element = plot_element ) } HistPlot <- function(color="darkgrey",obs=100){ new("HistPlot", plot_element = expr(hist(rnorm(!!obs), col = !!color, border = 'white')), color = color, obs = obs ) } #' Method to plot a Plot element setMethod("plotElement",signature = "AnyPlot",definition = function(object){ eval(object@plot_element) }) #' Method to render a Plot Element setMethod("shinyElement",signature = "AnyPlot",definition = function(object){ renderPlot(plotElement(object)) }) server <- function(input, output, session) { # Create a reactive to create the Report object report_obj <- reactive(HistPlot(obs=input$obs)) # Check for change of the slider to change the plots observeEvent(input$obs,{ output$renderedPDF <- renderText("") output$renderPlot <- shinyElement( report_obj() ) } ) } # Simple shiny App containing the standard histogram + PDF render and Download button ui <- fluidPage( sidebarLayout( sidebarPanel( sliderInput( "obs", "Number of observations:", min = 10, max = 500, value = 100) ), mainPanel( plotOutput("renderPlot") ) ) ) shinyApp(ui = ui, server = server) Standard shiny app server <- function(input, output) { # Output Gray Histogram output$distPlot <- renderPlot({ hist(rnorm(input$obs), col = 'darkgray', border = 'white') }) } # Simple shiny App containing the standard histogram + PDF render and Download button ui <- fluidPage( sidebarLayout( sidebarPanel( sliderInput( "obs", "Number of observations:", min = 10, max = 500, value = 100) ), mainPanel( plotOutput("distPlot") ) ) ) shinyApp(ui = ui, server = server) But an advantage of the object orientation is that you can now output the plot in a lot of different formats. We solved this by introducing methods called pdfElement , logElement , or archiveElement . To get a deeper look, you can check out some examples stored on GitHub here. These show differences between object-oriented and standard shiny apps. You can see that duplicated code is reduced in object-oriented apps, and while the code of the shiny app itself does not change for object-oriented apps, the code constructing the objects shown on the page changes. For the standard apps, the shiny code itself also changes every time an element is updated. The main advantage of this approach is that you can keep your shiny app exactly the same whatever it calculates or whatever it reports. Inside our department, this meant that whenever somebody wants a different plot inside an app, we do not have to touch our main app again. Whenever somebody wanted to change just the linear regression app, we did not have to touch other apps. The look and feel, the logging, and the PDF report stays exactly the same. Those three features shall never be touched unless an update is needed to their own functionality.

Packaging As you know, we did not build a singular app; we had to build many for the different mathematical analyses. So we decided that for each app we will construct a separate R package. This means we had to define one Class that defines what an app will look like in a core package. This can be seen as fitting into the Lego theme. So our app would be Lego City, where you have trucks and cars. Other apps may be more advanced and range inside Lego Technic. Now each contributor to our shiny app builds a package that contains a child of our core class. We called this class Module, and we have a lot of Module packages. This is not a Shiny module, but it’s modular. Our app now allows the bringing together a lot of those modules and making it bigger and bigger and bigger. It get’s more HP and I wouldn’t call it a car anymore. Yeah, we have a truck! Made of Lego bricks! Image by Barney Sharman The modularization and packaging now enables fast testing. Why? Each package can be tested using basic testthat functions. So first we tested our core application package, which allows adding building blocks. Afterwards, we tested each single package on its own. Finally, the whole application is tested. Our truck is ready to roll. Upon updates, we do not have to test the whole truck again. If we want to have larger tires, we just update the tire package, but not the core package or any other packages.