0
votes

I'm quite a beginner in R Shiny and I would like to create many multiple selectize inputs which are connected with each other. In other words : if an item is selected in one of the selectizeinputs i would like that it disappears from the other selectizeinputs' choices.

Below is an example of what i want (does not work)

 modalities <- LETTERS[1:10]

 library(shiny)

 app <- shinyApp(
   ui = tabPanel("Change modalities",
            selectizeInput("ui_mod_choose1", label=paste0("Modality 1"),choices=NULL, multiple = TRUE),
            selectizeInput("ui_mod_choose2", label=paste0("Modality 2"),choices=NULL, multiple = TRUE),
            selectizeInput("ui_mod_choose3", label=paste0("Modality 3"),choices=NULL, multiple = TRUE)
   ),

 server = function(input, output, session) {
   observe({
updateSelectizeInput(session,"ui_mod_choose1",choices=  modalities)
updateSelectizeInput(session,"ui_mod_choose2",choices=  modalities)
updateSelectizeInput(session,"ui_mod_choose3",choices=  modalities)
   })
 }

      )
 runApp(app)

EDIT : Here is a solution based on Bertil Nestorius' answer

 modalities <- LETTERS[1:10]

 library(shiny)

 app <- shinyApp(
   ui = tabPanel("Change modalities",
            numericInput("ui_number", label="Number of modalities",min = 1, max = 4, value=3),
            uiOutput("renderui")
   ),


   server = function(input, output, session) {


     output$renderui <- renderUI({
       output = tagList()
       for(i in 1:input$ui_number){
         output[[i]] = tagList()
         output[[i]][[1]] = selectizeInput(paste0("ui_mod_choose",i), label=paste0("Modality ",i),choices=modalities, multiple = TRUE)
  }
  return(output)
})


 lapply(
  X = 1:100, ####### QUESTION HERE
  FUN = function(j){
    observeEvent({
      input[[paste0("ui_mod_choose",j)]]
    },
    {
      sapply(1:input$ui_number,function(i){
        vecteur <- do.call(c,lapply((1:input$ui_number)[-i],function(i){input[[paste0("ui_mod_choose",i)]]}))
        updateSelectizeInput(session,paste0("ui_mod_choose",i),choices=  modalities[!modalities %in% vecteur],selected = input[[paste0("ui_mod_choose",i)]])
      })
    },
    ignoreNULL = FALSE)

  }
)


observeEvent({
  input$ui_num
},
{
  sapply(1:nput$ui_num,function(i){ 
    updateSelectizeInput(session,paste0("ui_mod_choose",i),choice= modalities,selected=NULL)
       })
     }
     )

   }

 )
 runApp(app)

The only problem I have left is on the following line :

  X = 1:100, ####### QUESTION HERE

See this issue for more information : lapply function using a numericInput parameter around an observeEvent in RShiny

2

2 Answers

1
votes

To have them all interconnected I would do something like this

modalities <- LETTERS[1:10]

library(shiny)

app <- shinyApp(
  ui = tabPanel("Change modalities",
                selectizeInput("ui_mod_choose1", label=paste0("Modality 1"),choices=NULL, multiple = TRUE),
                selectizeInput("ui_mod_choose2", label=paste0("Modality 2"),choices=NULL, multiple = TRUE),
                selectizeInput("ui_mod_choose3", label=paste0("Modality 3"),choices=NULL, multiple = TRUE)
  ),

  server = function(input, output, session) {
    observe({
      updateSelectizeInput(session,"ui_mod_choose1",choices=  modalities)
      updateSelectizeInput(session,"ui_mod_choose2",choices=  modalities)
      updateSelectizeInput(session,"ui_mod_choose3",choices=  modalities)
    })
    observeEvent({
      input$ui_mod_choose2
      input$ui_mod_choose3
    },
    {
      updateSelectizeInput(session,"ui_mod_choose1",choices=  modalities[!modalities %in% c(input$ui_mod_choose2,input$ui_mod_choose3)],selected = input$ui_mod_choose1)
    },
    ignoreNULL = FALSE)
    observeEvent({
      input$ui_mod_choose1
      input$ui_mod_choose3
    },
    {
      updateSelectizeInput(session,"ui_mod_choose2",choices=  modalities[!modalities %in% c(input$ui_mod_choose1,input$ui_mod_choose3)],selected = input$ui_mod_choose2)
    },
    ignoreNULL = FALSE)
    observeEvent({
      input$ui_mod_choose2
      input$ui_mod_choose1
    },
    {
      updateSelectizeInput(session,"ui_mod_choose3",choices=  modalities[!modalities %in% c(input$ui_mod_choose2,input$ui_mod_choose1)],selected = input$ui_mod_choose3)
    },
    ignoreNULL = FALSE)
  }

)
runApp(app)
0
votes

Something like this?

rm(list = ls())
library(shiny)
modalities <- LETTERS[1:10]

app <- shinyApp(
  ui = tabPanel("Change modalities",
                selectizeInput("ui_mod_choose1", label=paste0("Modality 1"),choices= modalities, multiple = TRUE),
                selectizeInput("ui_mod_choose2", label=paste0("Modality 2"),choices=NULL, multiple = TRUE),
                selectizeInput("ui_mod_choose3", label=paste0("Modality 3"),choices=NULL, multiple = TRUE)
  ),

  server = function(input, output, session) {

    observe({
      updateSelectizeInput(session,"ui_mod_choose2",choices =  modalities[!modalities%in% input$ui_mod_choose1])
    })
    observe({
      updateSelectizeInput(session,"ui_mod_choose3",choices =  modalities[!modalities %in% c(input$ui_mod_choose1,input$ui_mod_choose2)])
    })
  }
)
runApp(app)