代码之家  ›  专栏  ›  技术社区  ›  firmo23

禁用其他3个闪亮小部件的闪亮小部件选择

  •  0
  • firmo23  · 技术社区  · 1 年前

    我有 shiny 下面的应用程序有4个输入。它们包含的所有变量 mtcars 数据集。现在,我希望如果在一个输入中选择了一个值(例如第一个输入中的mpg),则该值不能在任何其他输入中选择。因此,每次这4个输入都会选择不同的值。

    library(shiny)
    library(shinydashboard)
    
    choices <- c("Pop", "RC", "RT","R4")
    
    ui <- dashboardPage(
      dashboardHeader(),
      dashboardSidebar(
        selectInput("Pr", "Select the price for analysis", choices = choices, multiple = F, selected = choices[1]),
        selectInput("Pr2", "Select the price for analysis", choices = choices, multiple = F, selected = choices[2]),
        selectInput("Pr3", "Select the price for analysis", choices = choices, multiple = F, selected = choices[3]),
        selectInput("Pr4", "Select the price for analysis", choices = choices, multiple = F, selected = choices[4])
        
      ),
      dashboardBody()
    )
    
    server <- function(input, output, session) {
      observeEvent(input$Pr, {
        updateSelectInput(session, "Pr2", choices = choices[!choices %in% input$Pr])
      })
      observeEvent(input$Pr3, {
        updateSelectInput(session, "Pr2", choices = choices[!choices %in%input$Pr3])
      })
      observeEvent(input$Pr4, {
        updateSelectInput(session, "Pr2", choices = choices[!choices %in%input$Pr4])
      })
      observeEvent(input$Pr2, {
        updateSelectInput(session, "Pr", choices = choices[!choices %in% input$Pr2])
      })
      observeEvent(input$Pr3, {
        updateSelectInput(session, "Pr", choices = choices[!choices %in% input$Pr3])
      })
      observeEvent(input$Pr4, {
        updateSelectInput(session, "Pr", choices = choices[!choices %in% input$Pr4])
      })
      observeEvent(input$Pr, {
        updateSelectInput(session, "Pr3", choices = choices[!choices %in% input$Pr])
      })
      observeEvent(input$Pr2, {
        updateSelectInput(session, "Pr3", choices = choices[!choices %in% input$Pr2])
      })
      observeEvent(input$Pr4, {
        updateSelectInput(session, "Pr3", choices = choices[!choices %in% input$Pr4])
      })
      observeEvent(input$Pr, {
        updateSelectInput(session, "Pr4", choices = choices[!choices %in% input$Pr])
      })
      observeEvent(input$Pr2, {
        updateSelectInput(session, "Pr4", choices = choices[!choices %in% input$Pr2])
      })
      observeEvent(input$Pr3, {
        updateSelectInput(session, "Pr4", choices = choices[!choices %in% input$Pr3])
      })
    }
    
    shinyApp(ui, server)
    
    1 回复  |  直到 1 年前
        1
  •  1
  •   YBS    1 年前

    试试这个

    library(shiny)
    library(shinydashboard)
    
    choices <- names(mtcars) # c("Pop", "RC", "RT","R4")
    
    ui <- dashboardPage(
      dashboardHeader(),
      dashboardSidebar(
        selectInput("Pr", "Select the price for analysis", choices = choices, multiple = F, selected = choices[1]),
        selectInput("Pr2", "Select the price for analysis", choices = choices, multiple = F, selected = choices[2]),
        selectInput("Pr3", "Select the price for analysis", choices = choices, multiple = F, selected = choices[3]),
        selectInput("Pr4", "Select the price for analysis", choices = choices, multiple = F, selected = choices[4])
        
      ),
      dashboardBody()
    )
    
    server <- function(input, output, session) {
      observeEvent(input$Pr, {
        choices1 <- choices[!choices %in% input$Pr]
        if (c(input$Pr) %in% input$Pr2) updateSelectInput(session, "Pr2", selected = choices1[1])
        choices2 <- choices[!choices %in% c(input$Pr,input$Pr2)]
        if (sum(c(input$Pr,input$Pr2) %in% input$Pr3)>0) updateSelectInput(session, "Pr3", selected = choices2[1])
        choices3 <- choices[!choices %in% c(input$Pr,input$Pr2,input$Pr3)]
        if (sum(c(input$Pr,input$Pr2,input$Pr3) %in% input$Pr4)>0) updateSelectInput(session, "Pr4", selected = choices3[1])
      })
      
      observeEvent(input$Pr2, {
        choices1 <- choices[!choices %in% input$Pr2]
        if (c(input$Pr) %in% input$Pr2) updateSelectInput(session, "Pr", selected = choices1[1])
        choices2 <- choices[!choices %in% c(input$Pr,input$Pr2)]
        if (sum(c(input$Pr,input$Pr2) %in% input$Pr3)>0) updateSelectInput(session, "Pr3", selected = choices2[1])
        choices3 <- choices[!choices %in% c(input$Pr,input$Pr2,input$Pr3)]
        if (sum(c(input$Pr,input$Pr2,input$Pr3) %in% input$Pr4)>0) updateSelectInput(session, "Pr4", selected = choices3[1])
      })
      
      observeEvent(input$Pr3, {
        choices1 <- choices[!choices %in% input$Pr3]
        if (c(input$Pr3) %in% input$Pr2) updateSelectInput(session, "Pr2", selected = choices1[1])
        choices2 <- choices[!choices %in% c(input$Pr3,input$Pr2)]
        if (sum(c(input$Pr3,input$Pr2) %in% input$Pr)>0) updateSelectInput(session, "Pr", selected = choices2[1])
        choices3 <- choices[!choices %in% c(input$Pr,input$Pr2,input$Pr3)]
        if (sum(c(input$Pr,input$Pr2,input$Pr3) %in% input$Pr4)>0) updateSelectInput(session, "Pr4", selected = choices3[1])
      })
      
      observeEvent(input$Pr4, {
        choices1 <- choices[!choices %in% input$Pr4]
        if (c(input$Pr4) %in% input$Pr2) updateSelectInput(session, "Pr2", selected = choices1[1])
        choices2 <- choices[!choices %in% c(input$Pr4,input$Pr2)]
        if (sum(c(input$Pr4,input$Pr2) %in% input$Pr3)>0) updateSelectInput(session, "Pr3", selected = choices2[1])
        choices3 <- choices[!choices %in% c(input$Pr4,input$Pr2,input$Pr3)]
        if (sum(c(input$Pr4,input$Pr2,input$Pr3) %in% input$Pr)>0) updateSelectInput(session, "Pr", selected = choices3[1])
      })
    
    }
    
    shinyApp(ui, server)