Shiny: multiple selectizeInput, one choice vector, discrete choices?

旧巷老猫 提交于 2019-12-14 02:07:31

问题


new to shiny. Hoping someone can help me out.

I have several selectizeInput's drawing choices from the same vector. My goal is to have values NOT show up in other Inputs after they have been chosen. ie. If value 1 has been chosen in Input 2, it should not be available in Input 1, 3 or 4.

ui<- shinyUI(fluidPage(

titlePanel("Selectize Test"),

sidebarPanel(
selectizeInput(
  "groupoptions1", "Group 1", choices = NULL, multiple = TRUE
),
selectizeInput(
  "groupoptions2", "Group 2", choices = NULL, multiple = TRUE
),
selectizeInput(
  "groupoptions3", "Group 3", choices = NULL, multiple = TRUE
),
selectizeInput(
  "groupoptions4", "Group 4", choices = NULL, multiple = TRUE
)

),

mainPanel(
  htmlOutput("grouplist")
)
))

server<- shinyServer(function(input, output, session) {

groupdata <- reactive({

  as.vector(1:30)

})

observe({
updateSelectizeInput(session, "groupoptions1", choices = groupdata())

updateSelectizeInput(session, "groupoptions2", choices = groupdata())

updateSelectizeInput(session, "groupoptions3", choices = groupdata())

updateSelectizeInput(session, "groupoptions4", choices = groupdata())
})


output$grouplist <- renderPrint({

  list(
    match(input$groupoptions1, groupdata()),
    match(input$groupoptions2, groupdata()),
    match(input$groupoptions3, groupdata()),
    match(input$groupoptions4, groupdata())
  )
})

})

I've tried having separate choice vectors for each selectizeInput that subtracts the other's selection, but then each time that vector updates all existing choices get wiped.

Any help greatly appreciated!


回答1:


You can and need to set the values when you update the input for the options inputs using the parameter selected:

 updateSelectizeInput(session, "groupoptions1", choices = groupdata1(),
                      selected=c(2,3))

You need to adapt the value according to your needs. I reduced your example to two list and moved all the relevant parts to observe

  observe({
    vals1<-input$groupoptions1
    vals2<-input$groupoptions2

    cat("updata input ")
    cat(isolate(vals1))
    cat(" | ")
    cat(isolate(groupdata2()))
    cat("\n")
    updateSelectizeInput(session, "groupoptions1",
                         choices =  as.vector(1:10)[! 1:10 %in% vals2], 
                         selected=vals1)
    updateSelectizeInput(session, "groupoptions2", 
                         choices =as.vector(1:10)[! 1:10 %in% vals1], 
                         selected=vals2)


  })


来源:https://stackoverflow.com/questions/34519213/shiny-multiple-selectizeinput-one-choice-vector-discrete-choices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!