This is how I think the shiny ui and server functions work

I've just been having some problems with a shiny app, that stemmed from my misunderstanding about how the execution order works. In particular accidental reexecution of a render function. This is because the server function in shiny can contain both non-reactive and reactive code. I think it works like this:

The server function is only run once.
All code (non-reactive and reactive) is executed in order, from beginning to end.
Reactive code sections (e.g. renderxxx, reactivexxx, observexxx ...) are also added to a special list.

Thereafter, shiny loops through this special list to see if any reactive section needs reexecuting because a reactive dependency has changed.

You can prevent the reexecution using req(), but be careful not to do this inside a renderxxx section, because this can result in a render which contains nothing (this is what I inadvertently did).

From what I understand, a renderxxx section returns a data structure (probably a complicated json list) that contains the essential information about the object, This data structure is sent to ui which knows how to turn it into html widgets.

My basic point is that the order of code in the server function does matter, and if you write it carefully you will get a smoother result and avoid difficult-to-find errors. Basically, try to write it so that all renderxxx sections produce the correct initial output first time through and no reactive dependencies remain to be triggered.

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.