Shiny cache causes delay in rendering with renderUI

Viewed 118

I have a Shiny app below, in which I am drawing a scatter plot on iris dataset using libraries highchart, ggplot and plotly.

library(shiny)
library(shinydashboard)
library(highcharter)
library(shinyWidgets)
library(plotly)
library(ggplot2)
library(data.table)

siderbar <- dashboardSidebar(
  sidebarMenu(
    # Add buttons to choose the way you want to select your data
    selectizeInput(inputId = "inp_species", label = "Select by:", choices = c("setosa", "versicolor", "virginica"), selected = "setosa"),
    awesomeRadio(inputId = "radioTest", label = "Choose one:",
                 choices=c("High Charter" = "highcharter",
                           "Simple Plot" = "simple",
                           "Plotly" = "plotly"),
                 inline = FALSE, selected = "highcharter")
  )   
)

body <- dashboardBody(
  fluidRow(
    tabBox(
      side = "right",
      selected = "Tab1",
      tabPanel("Tab1", "Tab content 1", uiOutput("tabset1Selected"))
    )
  ),
)

shinyApp(
  ui = dashboardPage(
    dashboardHeader(title = "tabBoxes"),
    siderbar,
    body
  ),
  
  server = function(input, output, session) {
    
  iris_dt <- reactive({
    iris_table = data.table(copy(iris))
    iris_table[Species == input$inp_species]
    })
  
   render_content <-  reactive({
     req(input$radioTest)
     print(input$radioTest)
      if(input$radioTest=='highcharter'){
      output$plot1 <-   renderHighchart({
        highchart() %>%
          hc_add_series(iris_dt(), type = "scatter", hcaes(x = Petal.Width, y = Sepal.Length))
        })
      out <- highchartOutput("plot1")
      }

      
      else if(input$radioTest=='plotly'){
        output$plot2 <- renderPlotly({
          plot_ly(iris_dt(), x = ~ Petal.Width, y = ~ Sepal.Length)
        })
        out <- plotlyOutput("plot2")
      }
     
     
     else if(input$radioTest=='simple'){
       output$plot3 <- renderPlot({
         ggplot(iris_dt(), aes(x =  Petal.Width, y = Sepal.Length)) + geom_point()
       })
       out <- plotOutput("plot3")
      
     }
     
     return(out)
    })
    
    
    # The currently selected tab from the first box
    output$tabset1Selected <-  renderUI({
      render_content()
    })
    

    
  }
)

I am selecting the library to draw the chart dynamically using a selectInput box.

Here is the problem -

  1. I select a species in selectInput box and the highchart library draws a scatter plot
  2. Then I select plotly in the radio button section and the rendering is done using plotly.
  3. I change the species in selectInput and plotly re-renders the plot
  4. Now, when I click on highchart radio button, the plot of the earlier species (from cache) is drawn for a few seconds and then the chart of the selected species is drawn.

Question Is there a way to clear or disable the cache so that the rendering delay does not happen?

1 Answers

We can disable the animation effect. Although it's not a solution for the problem, it could help in the meantime.

output$plot1 <- renderHighchart({
  highchart() %>%
    hc_add_series(
      data = iris_dt(),
      type = "scatter",
      hcaes(x = Petal.Width, y = Sepal.Length)
    ) %>%
    hc_plotOptions(
      series = list(
        animation = FALSE
      )
    )
})
Related