library(shiny) library(magrittr) shinyServer(function(input, output, session) { #__________________________________________________________________________________ # The main named list that will be used in many other tasks. listN <- reactiveValues() #__________________________________________________________________________________ observe({ # Trigger Add actions input$actionBtnAdd isolate({ new_selections <- input$add new_selections_name <- new_selections %>% paste(collapse = " X ") if(new_selections_name != "") listN[[new_selections_name]] <- new_selections }) updateSelectInput(session, inputId = "added", choices = names(listN)) }) #__________________________________________________________________________________ observe({ # Trigger Remove actions input$actionBtnAdded isolate({ # identify the items selected by user to be removed selections_to_remove <- input$added listNAsList <- list() # Here I manipulate 'listN' by converting it to list (using 'reactiveValuesToList()') if(!is.null(selections_to_remove)) { # To list so it can have their items removed listNAsList <- reactiveValuesToList(listN) # remove those items selected in input$added sapply(selections_to_remove, function(x) { listNAsList[[x]] <<- NULL return(NULL) }) %>% invisible } # Now, I'll try to update 'listN' by recreating it from scratch with only the remaining items in listNAsList object. list_names_for_choices <- names(listNAsList) if(is.null(list_names_for_choices)) { # if there is no items left... # ...an empty list is returned (without options, as desired) listN <- reactiveValues() # Here is a workaround to clear the 'added' list. NULL or character(0) would keep selectInput unchanged. list_names_for_choices <- "" } else { # if there is some remaining items on 'added' input list... # ...update listN with it. listN <- do.call(reactiveValues, listNAsList) } }) # Update select Input 'added' to show users the items created by them. updateSelectInput(session, inputId = "added", choices = list_names_for_choices) }) #__________________________________________________________________________________ # debug output to show the listN content. output$debug <- renderPrint({ listN %>% reactiveValuesToList }) })