Shiny modules; functions inside-vs-outside moduleServer

In every Shiny module guide I can find, the module server pattern is always present like so:

my_module_server <- function(id, ...) {
  moduleServer(input, output, session) {
    ## various functions, reactives, etc. here

But for functions and reactive elements that don't depend on input, output, session, it seems perfectly normal to actually include them outside the moduleServer call (relying on standard R lexical scoping), e.g.:

my_module_server <- function(id, rval) {  ## let rval be a reactiveVal passed from app

  rfun <- reactive({
    f(rval())  ## just a toy example, where f is some mutator on rval

  moduleServer(input, output, session) {
    ## various functions, reactives, etc. that depend on input, output, session here
    output[["foo"]] <- renderPrint(rfun())

In testing this works just fine and has the benefit of keeping elements not specific to input, output, or session separate from the moduleServer function itself in code, yet I don't ever seem to come across examples using this alternative pattern of simply placing additional objects inside the wrapping function but outside of moduleServer itself.

Are there any good reasons folks can think of where this latter pattern is discouraged somehow? (I've been using it to great effect for a while and haven't come across any particularly compelling reasons to not use it.)

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.