How to speed up plotly boxplots with many points and boxpoints = all

Viewed 155

I'm trying to write a shiny app where a dataset with about 30k rows is filtered by 3 user inputs. The outputs are:

  1. a leaflet map showing some spatial information for aggregated data values;
  2. a plotly boxplot showing the temporal trend of raw data values.

Clicking on each marker on the leaflet map highlights the corresponding values in the plotly boxplot, and selecting points on the latter plot, highlights the matching marker on the map. These two functionalities require showing all the raw data values in the boxplot.

The app I've written runs relatively smooth, so far. Unfortunately, the dataset is going to double its size in about 6 months and the project is (hopefully) going to last for 10 years, leading to a total of more than 600k estimated rows. Also, the app is going to be run by about 50 users simultaneously, so it needs to be fast and efficient.

At the moment, the slowest part seems to be printing all the data points with plotly. This is also showed by the following example with the diamonds dataset.

library(data.table)
library(shiny)
library(plotly)

my_data <- data.table(diamonds) %>% 
            setkey

min_carat <- min(my_data$carat)
max_carat <- max(my_data$carat)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      width = 3,
      sliderInput("carat",
                  label = "diamond carat",
                  min = min_carat,
                  max = max_carat,
                  value = max_carat)
    ),
    mainPanel(
      plotlyOutput("boxplot")
    )
  )
)

# Plotly Boxplot
server <- function(input, output, session) {
  
filtered_data <- reactive(my_data[carat <= input$carat,])
  
  output$boxplot <- renderPlotly({
    plot_ly(source = "boxplot") %>%
      add_trace(
        data = filtered_data(),
        x = ~cut,
        y = ~price,
        color = ~I("gray"),
        marker = list(
          color = "black"
        ),
        line = list(color = "black"),
        type = "box",
        boxpoints = "all",
        pointpos = 0,
        jitter = 0.5
      ) %>%
      layout(
        xaxis = list(
          fixedrange = TRUE
        ),
        yaxis = list(
          fixedrange = TRUE
        )
      )
  })

}

# Run the application
shinyApp(ui = ui, server = server)

Unfortunately, for the box traces, scattergl seems to be not available (Issue #2534) and scatter traces don't have a jitter option (Issue #299).

Any ideas or workarounds to improve the performances of the app?

Thanks for your help.

0 Answers
Related