title: shiny module inside module loosing reactive value
body: <p>I am trying to reuse the <em>Filter module</em> code provided in the <a href="https://mastering-shiny.org/scaling-modules.html#dynamic-ui" rel="nofollow noreferrer">Mastering Shiny</a> book. It takes a dataframe, generate a &quot;select&quot; widget for each column and return a <code>reactive</code> that output a boolean vector. This vector can be used to filter the dataframe row-wise according to the selected data range from the widgets.</p>
  <p>This works as intended when I simply reuse the filter module directly in a ShinyApp.
  But when I try to use it from inside another module, the returned <code>reactive</code> outputs <code>logical(0)</code> where it should output a vector with length equal to row number of the input dataframe.</p>
  <p>Here is a minimal working example based on the code from the book.</p>
  <pre class="lang-r prettyprint-override"><code>library(shiny)
  library(purrr)
  
  # Filter module from https://mastering-shiny.org/scaling-modules.html#dynamic-ui
  
  #helper functions
  make_ui &lt;- function(x, id, var) {
    if (is.numeric(x)) {
      rng &lt;- range(x, na.rm = TRUE)
      sliderInput(id, var, min = rng[1], max = rng[2], value = rng)
    } else if (is.factor(x)) {
      levs &lt;- levels(x)
      selectInput(id, var, choices = levs, selected = levs, multiple = TRUE)
    } else {
      # Not supported
      NULL
    }
  }
  filter_var &lt;- function(x, val) {
    if (is.numeric(x)) {
      !is.na(x) &amp; x &gt;= val[1] &amp; x &lt;= val[2]
    } else if (is.factor(x)) {
      x %in% val
    } else {
      # No control, so don't filter
      TRUE
    }
  }
  
  #Module
  filterUI &lt;- function(id) {
    uiOutput(NS(id, &quot;controls&quot;))
  }
  filterServer &lt;- function(id, df) {
    moduleServer(id, function(input, output, session) {
      vars &lt;- reactive(names(df))
      
      output$controls &lt;- renderUI({
        map(vars(), function(var) make_ui(df[[var]], NS(id, var), var))
      })
      
      reactive({
        each_var &lt;- map(vars(), function(var) filter_var(df[[var]], input[[var]]))
        reduce(each_var, `&amp;`)
      })
    })
  }
  
  #App
  filterApp &lt;- function() {
    ui &lt;- fluidPage(
      sidebarLayout(
        sidebarPanel(
          textOutput(&quot;n&quot;),
          filterUI(&quot;filter&quot;),
        ),
        mainPanel(
          verbatimTextOutput(&quot;debug&quot;),
          tableOutput(&quot;table&quot;)    
        )
      )
    )
    server &lt;- function(input, output, session) {
      df &lt;- datasets::ToothGrowth[seq(1,60,5),] #subset rows from ToothGrowth
      filter &lt;- filterServer(&quot;filter&quot;, df)
      
      output$table &lt;- renderTable(df[filter(), , drop = FALSE])
      output$n &lt;- renderText(paste0(sum(filter()), &quot; rows&quot;))
      output$debug &lt;- renderPrint(filter())
    }
    shinyApp(ui, server)
  }
  
  filterApp() #This works !
  </code></pre>
  <p>Then a simple Module to test the filter from inside it :</p>
  <pre class="lang-r prettyprint-override"><code>filterPageUI &lt;- function(id) {
    tagList(
      filterUI(NS(id, &quot;filter&quot;))
    )
  }
  filterPageServer &lt;- function(id, df) {
    moduleServer(id, function(input, output, session) {
      filterServer(&quot;filter&quot;, df = df)
    })
  }
  </code></pre>
  <p>And the ShinyApp modification to use this new module :</p>
  <pre class="lang-r prettyprint-override"><code>filterPageApp &lt;- function() {
    ui &lt;- fluidPage(
      sidebarLayout(
        sidebarPanel(
          textOutput(&quot;n&quot;),
          filterPageUI(&quot;filterpage&quot;),
        ),
        mainPanel(
          verbatimTextOutput(&quot;debug&quot;),
          tableOutput(&quot;table&quot;)    
        )
      )
    )
    server &lt;- function(input, output, session) {
      #subset rows from ToothGrowth
      df &lt;- datasets::ToothGrowth[seq(1,60,5),]
      filter &lt;- filterPageServer(&quot;filterpage&quot;, df)
      
      output$table &lt;- renderTable(df[filter(), , drop = FALSE])
      output$n &lt;- renderText(paste0(sum(filter()), &quot; rows&quot;))
      output$debug &lt;- renderPrint(filter())
    }
    shinyApp(ui, server)
  }
  
  filterPageApp() #This does not work!
  </code></pre>
  <p>I suspect that the problem comes from namespacing, maybe inside the map/reduce logic. But I cant wrap my head around it.
  Moreover, the last paragraph from the chapter says that this module should be usable elsewhere without modifications.</p>
  <blockquote>
  <p>A big advantage of using a module here is that it wraps up a bunch of advanced Shiny programming techniques. You can use the filter module without having to understand the dynamic UI and functional programming techniques that make it work.</p>
  </blockquote>
  <p>Any advice would be greatly appreciated. Thanks in advance !</p>