R Plotly scatter plot with size assigned to a variable crashes browser with > ~10000 points

Hello, I’m having an issue with R plotly scatterplots crashing my chrome or edge browser. It appears to load OK if you only have about 10000 points or less. My data set usually is between 15K-50K and it’s meant for the user to see general trends of the data but then filter it down, but visualizing all those points is important. Below is a reproducible example, you can adjust the number of points generated in the dataset between 50K and 10K to see the performance difference along with turning marker = list(size = ~size_var_num), on and off to see without that it will load in chrome. As an FYI everything runs ok in the RStudio app preview window, but once you go to chrome it won’t load and usually crashes the browser completely and I have to use task manager to close it out.

Is this expected for data sets of this size or is there a solution where the size works at 50K records and won’t crash chrome? I see the same issue when the app is deployed to an RStudio Connect server. I have also posted this topic on the Plotly Community site here.

See code below, appreciate any thoughts!

library(shiny)
library(plotly)
library(tidyverse)

data_set <- tibble(xvar = runif(50000), yvar = runif(50000), sizevar = runif(50000)) %>% 
#data_set <- tibble(xvar = runif(10000), yvar = runif(10000), sizevar = runif(10000)) %>% 
  mutate(size_var_num = case_when(
           sizevar < .2 ~ 5,
           sizevar < .4 ~ 10,
           sizevar < .6 ~ 15,
           sizevar < .8 ~ 20,
           TRUE ~ 25
         ))

ui <- fluidPage(
  headerPanel('Example'),
  sidebarPanel(shinyWidgets::noUiSliderInput(
                 inputId = "xvar_slider",
                 label = "xvar slider : ",
                 behaviour = "drag",
                 min = 0,
                 max = 1,
                 value = c(0,100),
                 step = .001,
                 color = "#3c8dbc"
               )),
  mainPanel(
    plotlyOutput('plot')
  )
)

server <- function(input, output) {
  
  data <- reactive({
    
    data_set %>% 
      filter(xvar >= input$xvar_slider[1] & xvar <= input$xvar_slider[2]) %>% 
      mutate(label = paste0("xvar: ", xvar, "\nyvar: ", yvar, "\nxvar percent: ", round(xvar,2)*100, 
                            "%\nyvar dollar: $", format(yvar*100000, big.mark=",")))

  })
  
  output$plot <- renderPlotly(
    
    data() %>% 
      plot_ly(
      x = ~xvar,
      y = ~yvar, 
      marker = list(size = ~size_var_num), #, sizemode = 'diameter', sizeref = 2.5
      opacity = 1,
      type = 'scatter',
      mode = 'markers',
      hoverinfo = 'text',
      hovertext = ~label)
  )
  
}

shinyApp(ui,server)

It never hurts to have the latest package versions , for plotly I think this would be 4.10.0 ; recommend checking you are up to date.
Also a quick win might be to throw %>% toWebGL() on the end

  output$plot <- renderPlotly(
    
    data() %>% 
      plot_ly(
        x = ~xvar,
        y = ~yvar, 
        marker = list(size = ~size_var_num), #, sizemode = 'diameter', sizeref = 2.5
        opacity = 1,
        type = 'scatter',
        mode = 'markers',
        hoverinfo = 'text',
        hovertext = ~label) %>% toWebGL()
  )

I had plotly 4.10.0, but did not have toWebGL(), that actually appears to have worked! Ran several tests will mark that as the solution and will read more on that function. Thanks!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.