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