How to get a list of Lists in Shiny


#1

it seems to me that the problem is that params$btn is always = 1, and that is what the append is not happening, is there anyway of increasing the value of params$btn by 1 the next time the botton is pressed to override the first list selection and append the new one? In other words, the first input is "a,"b, the second input from the second drop down menu after pressing the button again is "a", "b", "c". I would like to get
[1] "a" "b" "a" "b" "c" instead of [1] "a" "b" "a" "b" "a" "b" "c". It seems that the first "a"b"c" is appended twice.
In other words: press button and the first selection is "a","b". Press button again and the second selection is "a","b","c"
What I am trying to do is to append list("a","b","c ") to list("a","b" ) to get a list of two lists : [1] "a","b 2[] "a","b","c"

library(shiny)
library(shinyjs)
firstUI <- function(id) { uiOutput(NS(id, "first")) }

firstServer <- function(input, output, session){#, a) {
  ns = session$ns
  # returnedValue = NULL
  output$first <- renderUI({
    
    # selectInput(ns("select"), h4("Select"), paste0(isolate(a()), letters[1:4]))
    selectInput(ns("select"), h5(strong("Please select from the list")), choices = c("","a", "b", "c", "d"), multiple=TRUE, selectize=TRUE)
    
  })

  returnedValue <- reactive({paste0(input$select)})
  return(returnedValue)
}


removeFirstUI <- function(id) {
  removeUI(selector = paste0('#', NS(id, "first")))
}

addRmBtnUI <- function(id) {
  ns <- NS(id)
  tags$div(
    actionButton(inputId = ns('insertParamBtn'), label = "Add"),
    actionButton(ns('removeParamBtn'), label = "Remove"),
    hr(),
    tags$div(id = ns('placeholder'))
  )
}

addRmBtnServer <- function(input, output, session, moduleToReplicate, ...) {
  ns = session$ns
  params <- reactiveValues(btn = 0, inputFiles = list())
  observeEvent(input$insertParamBtn, {
    params$btn <- params$btn + 1
    params$inputFiles[[params$btn]] <- callModule(moduleToReplicate$server, id = params$btn, ...)
    insertUI(
      selector = paste0('#', ns('placeholder')),
      ui = moduleToReplicate$ui(ns(params$btn))
    )
  })
  
  observeEvent(input$removeParamBtn, {
    moduleToReplicate$remover(ns(params$btn))
    params$btn <- params$btn - 1
  })
  
  return(params)
}
ui <- fluidPage(
  addRmBtnUI("addRm"),
verbatimTextOutput("text", placeholder = TRUE),
actionButton("saveBTN", "Press the save button to end"),
verbatimTextOutput("list"),
verbatimTextOutput("listOfLists")

)
server <- function(input, output, session){
  
  comp <- callModule(
    addRmBtnServer, id = "addRm",
    moduleToReplicate = list(
      ui = firstUI,
      server = firstServer,
      remover = removeFirstUI
    )
  )
 
   myValues <- reactiveValues()
    observe({
      if(comp$btn>0 && length(comp$inputFiles[[comp$btn]]())  > 1){
      myValues$fList <- c(isolate(myValues$fList), c(comp$inputFiles[[comp$btn]]()))
        
      }
    })
    
    moreValues <- reactiveValues()
    observe(
      moreValues$anotherList <- c(isolate(moreValues$anotherList))
    )
    
 output$listOfLists<-renderPrint({
   myList  <- myValues$fList
   secondList <- moreValues$anotherList
   theList <- list()
  if(comp$btn>0 && length(comp$inputFiles[[comp$btn]]())  > 1)
  # myValues <- calllist(c(comp$inputFiles[[comp$btn]](), comp$inputFiles[[comp$btn]]() ))
   theList <- myValues$fList
   
   theList
})
  
}

shinyApp(ui = ui, server = server)

OUTPUT:

Please select from the list
a
b
Please select from the list
a
b
c

[1] "a" "b" "a" "b" "a" "b" "c"