Hi all,
I'm trying to implement bindCache to make my shiny app faster. There is a long running data query that sometimes fails. I am using validate(need(..)) to check to see if it fails and wait, but bindCache still seems to write. This means if I want to try it again, the cache kicks in with the error message from the need(). Is there a better workflow for this to let the app try again if there's an error? Or alternatively, can I invalidate the cache easily for a particular key? A dummy example of bindCache still writing with need is below.
library(shiny)
library(tidyverse)
shinyOptions(cache = cachem::cache_disk("cache"), max_age = 60 * 60 * 24)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$distPlot <- renderPlot({
validate(
need(1 == 2, 'hmm')
)
print("hi")
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
}) %>% bindCache(input$bins)
}
# Run the application
shinyApp(ui = ui, server = server)
Shiny applications not supported in static R Markdown documents
library(shiny)
library(tidyverse)
ch <- cachem::cache_disk("cache")
ch$reset()
shinyOptions(cache =ch , max_age = 60 * 60 * 24)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30),
shiny::checkboxInput("condition","Validation Condition",
value=FALSE)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
validation_check1 <- reactive({
TRUE == input$condition
})
output$distPlot <- renderPlot({
validate(need(validation_check1(),"hmm"))
req(validation_check1())
Sys.sleep(1)
print("hi")
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
}) %>% bindCache(input$bins,validation_check1())
}
# Run the application
shinyApp(ui = ui, server = server)
Thanks for the suggestion. So essentially pull out the validation and use that as a key for the cache as well. I think this should work. I am curious if there's a way to either avoid caching entirely, or able to get the cache key and delete it just for my own curiosity. Thanks again!