Here I have tried to replicate my original problem. The following shiny code will create 'Sub Segment' button if you click on 'Add a Segment'.
Now 'Add a Sub Segment' button should give a single line text on each click. But what I have noticed, It creates (1 + number of 'Add a Segment' button after the clicked one) many lines.
For single observeEvent it is working (the code is commented out).
library(shiny)
ui <- fluidPage(
verbatimTextOutput("txt",placeholder = T), #"It is Created for Testing"
actionButton("addSeg", "Add a Segment"),
uiOutput("myUI")
)
server <- function(input, output, session) {
alld <- reactiveValues()
alld$ui <- list()
# Action to add new Segment
observeEvent(input$addSeg,{
alld$ui[[length(alld$ui)+1]] <- list(actionButton(paste0("addSub_",(length(alld$ui)+1)), "Add a Sub Segment"))
})
# Action to add new Sub Segment
# observeEvent(input[[paste0("addSub_",1)]],{
# alld$ui[[1]][[length(alld$ui[[1]])+1]] <- paste0("addSub_",1)
# })
observeEvent(input$addSeg,{
lapply(1:length(alld$ui), function(i){
observeEvent(input[[paste0("addSub_",i)]],{
alld$ui[[i]][[length(alld$ui[[i]])+1]] <- HTML(paste0("<br>addSub_",i,"<br>"))
})
})
})
output$myUI <- renderUI({alld$ui})
output$txt <- renderText(class(alld$ui))
}
shinyApp(ui, server)
Please Help...