Observe reactives with levels

I am currently developing a dashboard that is supposed to do a lot of things. Hence it consists of a lot of modules. Currently I have an issue with observeEvents with a reactive list that consists of different reactive lists.

Just to make it clear, I know why my code doesnt work. The reactive d() does only change when I append the new indicies with a new reactive list are added. The actual reactive expression inside each list is not evaluated because the observeEvent only looks after the first reactiveElement.

I have made some sort of example with my issue. I have tried looking at shiny::reactiveValues(). My thought was somehow to use that instead of a list, but I am unsure about how to update it in the module_1function.

The problem is that I do NOT know how the list is beforehand, and I would very much AVOID doing something like

shiny::observe({
      lapply(seq_along(d()), function(i) {
        shiny::observeEvent(d()[[i]](), {
          shiny::req(d())
          browser()
        })
      })
    })

Thank you in advance :slight_smile:

server <- function(input, output, session) {
  d <- module_1("mod_layer1")
  #... a lot more modules
  
 shiny:: observeEvent(d(), {
    ##DOES NOT TRIGGER - OBVIOUSLY - WHEN VAR1,VAR2,VAR3 changes in the different session
    ##This is only triggered when d() is appended
    print("trigger")
  })
}

module_1 <- function(id) {
  moduleServer(id, function(input, output, session) {
    ns <- session$ns
    reac_returned <- shiny::reactiveVal(vector("list", 0L))
    
    shiny::observeEvent(input$button, {
      shinyjs::disable(id = "button")
      for(i in 1:input$no){
        new_id <- ns(paste0("mod_layer2", i))
        
        shiny::insertUI(
          ui <- UI_function(new_id)
        )
        
        toAppend <- reac_returned() %>% append(layer_n(paste0("mod_layer2", i)))
        reac_returned(toAppend)
      }
    })
  })
}


layer_n <- function(id){
  moduleServer(id, function(input, output, session) {
    ns <- session$ns
    
    shiny::reactive({
      list(
        var1 = input$var1,
        var2 = input$var2
        var3 = input$var3
      )
    })
  })
}

This topic was automatically closed 90 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.