Conditional caching on shiny's bindCache

Viewed 213

Let's say I have a reactive like this somewhere in my shiny server:

my_reactive <- reactive({
    build_result(input$x)
})

As of Shiny 1.6, I can cache its result with bindCache like this:

my_reactive <- reactive({
    build_result(input$x)
}) %>% bindCache(input$x)

Now let's say that I don't know if I should cache the result before I get the result of build_result.

build_result("input")
# $should_cache
# [1] FALSE
# 
# $result
# [1] "output"

Is there any way to tell bind_cache to cache the result of this reactive's evaluation only when should_cache is TRUE?
In case someone is curious as to why I would want that, build_result calls an API where should_cache==FALSE actually means "I need more time to compute this, this is only a temporary result, next time you ask it will be better, but I didn't want to block the whole server, so here's what I have so far." (yeah, my APIs can be quite chatty).

0 Answers
Related