Selectizeinput输入是相互排斥的R Shiny

时间:2018-05-29 17:57:38

标签: r input shiny

我必须为同一个变量使用多个selectizeinput。当我选择一个类别一个bla1时,该类别应排除在bla2中。我如何实现这一目标?是否可以选择链接两个选择性输入?

ui <- fluidPage(

   # Application title
   titlePanel("Old Faithful Geyser Data"),

   # Sidebar with a slider input for number of bins 
   sidebarLayout(
      sidebarPanel(
         sliderInput("bins",
                     "Number of bins:",
                     min = 1,
                     max = 50,
                     value = 30),
         selectizeInput("bla1", "muh", choices = faithful$waiting, multiple = TRUE),
         selectizeInput("bla2", "muh2", choices = faithful$waiting, multiple = TRUE)
      ),

      # Show a plot of the generated distribution
      mainPanel(
         plotOutput("distPlot")
      )
   )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

   output$distPlot <- renderPlot({
      # generate bins based on input$bins from ui.R
      x    <- faithful[, 2] 
      bins <- seq(min(x), max(x), length.out = input$bins + 1)

      # draw the histogram with the specified number of bins
      hist(x, breaks = bins, col = 'darkgray', border = 'white')
   })
}

# Run the application 
shinyApp(ui = ui, server = server)

2 个答案:

答案 0 :(得分:1)

ui <- fluidPage(

  # Application title
  titlePanel("Old Faithful Geyser Data"),

  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
      sliderInput("bins",
                  "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30),
      selectizeInput("bla1", "muh", choices = faithful$waiting, multiple = TRUE),
      htmlOutput("bla2")
    ),

    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("distPlot")
    )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output) {



  output$bla2 <- renderUI({
    ## filter choices to anything NOT selected by bla1
    choices <- faithful$waiting[!faithful$waiting %in% input$bla1]
    selected <- input$bla2
    selectizeInput("bla2", "muh2", choices = choices, multiple = TRUE, selected = selected)
  })

  output$distPlot <- renderPlot({
    # generate bins based on input$bins from ui.R
    x    <- faithful[, 2] 
    bins <- seq(min(x), max(x), length.out = input$bins + 1)

    # draw the histogram with the specified number of bins
    hist(x, breaks = bins, col = 'darkgray', border = 'white')
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

此代码由一个人发布,这是最好的解决方案。唯一的事情就是当我点击&#34;输入$ bla2&#34;当我输入一个值时,我会失去对该领域的关注。可能是因为它每次都会再次呈现。任何人都知道如何克服这个问题?

答案 1 :(得分:0)

首先需要在服务器端定义输入。 然后,只需要一个小技巧来获得avaiable选项:

ui <- fluidPage(

  # Application title
  titlePanel("Old Faithful Geyser Data"),

  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
      sliderInput("bins",
                  "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30),
      uiOutput("bla1_ui"),  # here just for defining your ui
      uiOutput("bla2_ui")
    ),

    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("distPlot")
    )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
  # and here you deal with your desired input
  output$bla1_ui <- renderUI({
    selectizeInput("bla1", "muh", choices = faithful$waiting, multiple = TRUE)
  })

  output$bla2_ui <- renderUI({

    avaiable <- faithful$waiting
    if(!is.null(input$bla1))
      avaiable <- faithful$waiting[-which(faithful$waiting %in% input$bla1)]

    selectizeInput("bla2", "muh2", choices=avaiable, multiple = TRUE)
  })

  output$distPlot <- renderPlot({
    # generate bins based on input$bins from ui.R
    x    <- faithful[, 2] 
    bins <- seq(min(x), max(x), length.out = input$bins + 1)

    # draw the histogram with the specified number of bins
    hist(x, breaks = bins, col = 'darkgray', border = 'white')
  })
}

# Run the application 
shinyApp(ui = ui, server = server)