Is there any way to pre-cache output in Shiny?
How about using a persistent cache, running the app once, where you manually change all inputs as needed (I also included an automated version, which I am not totally happy with, b/c race conditions could occur) and then in subsequent runs you have all the values properly cached?
library(shiny)
library(magrittr)
## change path to a non temp diretcory to keep that even after reboot
shinyOptions(cache = cachem::cache_disk(file.path(dirname(tempdir()),
"myapp-cache")))
xl <- 1:3
yl <- 1:3
ui <- fluidPage(
sliderInput("x", "x", min(xl), max(xl), min(xl), 1),
sliderInput("y", "y", min(yl), max(yl), min(yl), 1),
verbatimTextOutput("z"),
actionButton("fill", "Fill Cache")
)
server <- function(input, output, session) {
idx <- idy <- 1
r <- reactive({
message("Doing expensive computation...")
Sys.sleep(2) ## simulate expensive op
input$x + input$y
}) %>% bindCache(input$x, input$y)
observe({
req(input$fill)
if (idx != length(xl) + 1 || idy != length(yl)) {
## need the invalidateLater approach
## to allow shiny reacting on the change
## not sure whether we cannot trip over race conditions
## recommendation: do it once by hand (it's persistent anyways ;)
invalidateLater(500, session)
if (idx == length(xl) + 1) {
message("Updating y:", idy)
idx <<- 1
idy <<- idy + 1
updateSliderInput(session, "y", value = yl[[idy]])
} else {
message("Updating x:", idx)
updateSliderInput(session, "x", value = xl[[idx]])
idx <<- idx + 1
}
}
})
output$z <- renderText(r())
}
## Start app and set all values
shinyApp(ui, server)
## Close app and restart
## Cache is now filled
shinyApp(ui, server)
How to prevent shinyapps.io caching?
I've found the similar problem here (no idea how I missed it before)
It seems that moving all the data fetching to the
server <- function(){}
resolved the problem.
Setting http headers for caching with shiny server & nginx
Solved it with the help of the following instructions: https://www.digitalocean.com/community/tutorials/how-to-implement-browser-caching-with-nginx-s-header-module-on-ubuntu-16-04
# Expires map
map $sent_http_content_type $expires {
default off;
text/html epoch;
text/css max;
application/javascript max;
~image/ max;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
expires $expires;
. . .
Related Topics
Split Concatenated Column to Corresponding Column Positions
How to Configure Box.Color in Directlabels "Draw.Rects"
Grouped Correlation with Dplyr (Works Only on Console)
Incorrect Number of Subscripts on Matrix in R
Tidyr Separate Only First N Instances
How to Place an Image in an R Shiny Title
Outputing N Tables in Shiny, Where N Depends on the Data
Nan Is Removed When Using Na.Rm=True
Memory Limits in Data Table: Negative Length Vectors Are Not Allowed
Dplyr 0.7 Equivalent for Deprecated Mutate_
R: Generating All Permutations of N Weights in Multiples of P
R Data.Table Join: SQL "Select *" Alike Syntax in Joined Tables