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 -
- I select a species in
selectInputbox and thehighchartlibrary draws a scatter plot - Then I select
plotlyin the radio button section and the rendering is done using plotly. - I change the species in
selectInputandplotlyre-renders the plot - Now, when I click on
highchartradio 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?