0

I am trying to use nested modules in a shiny flexdashboard. The code works well in generating dynamically input and outputs, but I am having problems using actions on them in observers (also to be generated by module).

I had seen questions regarding nesting modules and UI elements, and some related to reactives (here, for example). But I did not find any related to dynamically generated observers for those UI elements...

The simplified code I am using is:

# inner module
DefVarsOutput<-function(input, output, session, VarsToIdentify){
  ns = session$ns
  len<- length(VarsToIdentify)
  #DefVarsOutputUI(PatentData,VarsToIdentify)

  output$DefVars<-renderUI({
    tagList(
      wellPanel( 
        lapply(1:len, function (x) {
          wellPanel( 
            p(paste0("Variable for ",VarsToIdentify[x])),
            splitLayout(
              cellWidths = c("5%","5%", "90%"),
              actionButton(ns(paste0("Def_",VarsToIdentify[x])), 
                           icon("arrow-right", class = NULL, lib = "font-awesome"),
                           style='padding:4px; font-size:80%'),
              wellPanel(style = "background-color: #ffffff;",
                        textOutput(ns(paste0("Selected_", VarsToIdentify[x])))
              )
            ))
        }))
    )})
  renderVarsObservers <-reactive(
    lapply(1:len, function (x) {
      observeEvent(input[[paste0("Def_", VarsToIdentify[x])]], {
        SelectedColX<-as.character(Columns[req(input$DataVars_row_last_clicked)])
        output[[paste0("Selected_",VarsToIdentify[x])]]<-renderText(SelectedColX)
      }, ignoreInit = TRUE)

    })
  )

  return( renderVarsObservers())

} 

UI.DefVarsOutput <- function(id,label = "Add Selection bars")     {
  ns <-  NS(id)
  tagList(
    uiOutput(ns("DefVars"))
  )
}     


# outer module
NewDataFormat <- function(input, output, session, VarsToIdentify) {
  ns<-session$ns
  #......
  DefVarsOutput2<-callModule(DefVarsOutput, VarsToIdentify=VarsToIdentify, "NewDataFormat2")
  DefVarsOutput2
  output$DefVarsUI<-renderUI({
    tagList(
      UI.DefVarsOutput(ns("NewDataFormat2") )
    )
  })
}

UI.NewDataFormat <- function(id, label = "Add New Data Format") {
  # Create a namespace function using the provided id
  ns <- NS(id)

  tagList(
    wellPanel( style='padding:4px',
               #tags$div( id = 'FormatError')
               uiOutput(ns("DefVarsUI"))
    )
  )

}

The code worked well before I tried to convert them in nested modules (just one level of module)...

I used ns() naming the UI elements, and they are correctly generated, but no action happens when I click on the actionbuttons.

The code input[[name]] appears to work when using as input for other UI elements in an outer module in this link and worked when it was in an not nested (or outer) module...

So I think it is related to how observers are passed to the outer module, or UI elements identification is modified (maybe something related to session)... I tried some options but was not able to figure out yet... How must we generate or pass the observers for/using dynamically generated UI elements?

Any help is appreciated. Thanks in advance

Boxuan
  • 4,937
  • 6
  • 37
  • 73
hamagust
  • 728
  • 2
  • 10
  • 28
  • HI, your problem at least with the code you supplied here is that `DefVarsOutput2` is never called and since it is a reactive statement it is not evaluated until it is called, hence the observers are never initiated. you will see this if you place an browser with in the reactive function. At the moment I don't really know a good solution for this though – Bertil Baron Jul 05 '18 at 08:47
  • Hi, Bertil, thank you very much for your comment. I thought I was calling `DefVarsOutput2` with `DefVarsOutput2<-callModule(DefVarsOutput, VarsToIdentify=VarsToIdentify, "NewDataFormat2") DefVarsOutput2`... I found a related question [here](https://stackoverflow.com/questions/45169876/observeevent-shiny-function-used-in-a-module-does-not-work)... they put the input inside `reactive`, but in their case, the input is inside the `handlerExpr`, not `eventExpr`... I tried, but yet without a solution... – hamagust Jul 05 '18 at 12:11

0 Answers0