I came up with a little solution to a little problem I encountered, and am sharing it in the thought that others might find it useful and save themselves a bit of time.
I have some expensive processing that I would like to be performed once at start-up, and then again if and only if the user so requests. I tried using the new isolate() function that Joe introduced yesterday. His example shows how to keep expensive code from starting up until a specified action button is pressed. The problem: I wanted the code to run exactly once before the action button was pressed, but then subsequently only if and when the action button was pressed.
Joe's example code wouldn't quite do the trick: an action button's count variable is automatically set initially to zero, so "if(input$buttonValue==0) return(NULL)" won't run my code for me at start up. (Sidebar note/feature request: It would be nice if it were possible to create action buttons with non-zero initial values.)
The solution, like so many others, is obvious once you see it: don't use zero as the test value.
expensiveProcess <- reactive(function() {
if (input$recalcButton == -1)
return(NULL)
return(isolate({
# reset value of action button to prevent further un-requested recomputation of the expensive process
input$recalcButton <- -1
# Now do the expensive stuff
foo <- activateInterlock(input$foo)
bar <- connectDynotherm(input$bar)
baz <- bringInfracellsUp(input$baz)
c(foo=foo, bar=bar, baz=baz)
}))
})
Some readers may react by thinking, "Well, no duh." What can I say: it took me a bit to figure it out (employing my currently flu-fogged brain), and thought this structure might be of use to some others.
Regards,
Arthur