Selectizeinput 输入互斥 R Shiny

Posted

技术标签:

【中文标题】Selectizeinput 输入互斥 R Shiny【英文标题】:Selectizeinput inputs be mutually exclusive R Shiny 【发布时间】:2018-11-08 11:00:10 【问题描述】:

我必须为同一个变量使用多个选择输入。当我选择一个类别 bla1 时,该类别应排除在 bla2 中。我该如何存档?是否有链接两个 selectizeinputs 的选项?

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)

【问题讨论】:

您必须通过renderUI 在服务器中创建selectizeInput 并包含一些过滤逻辑。 【参考方案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)

此代码是由一个人发布的,它是最好的解决方案。唯一的事情是,当我单击“input$bla2”时,当我输入一个值时,我失去了对字段的关注。可能是因为它每次都会重新渲染。有人知道如何克服这个问题吗?

【讨论】:

虽然不是真正的互斥,bla1 将始终拥有完整的选项并确定bla2 的选项。您的意思是关注“bla2”,因为每次选择后下拉菜单都会关闭?可能一些 css 或 JS 可以解决问题。【参考方案2】:

您首先需要在服务器端定义您的输入。 然后,只需做一个小技巧即可获得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)

【讨论】:

这将在再次调用 bla1 时重新渲染 bla2。可能reactive/reactiveVal/reactiveValues 可能会有所帮助? 我不明白你的意思。有一个错误,如果你不选择一个bla1,bla2是空的,但我会用设置变量avaiable的条件来纠正它 从第一个 selectInput 中选择一些内容,然后从第二个 selectInput 中选择一些内容,然后再从第一个中选择。您将看到,第二个 selectInput 中的选定变量已消失。 嗯,你是对的,关于 reactive 解决这个问题也是对的。但是我对reactive不是很了解,我使用的时候是通过反复试验。【参考方案3】:

在对isolate() 进行了一些实验之后,我想我找到了一个不错的解决方案。你可以试试:

shiny::runGist("https://gist.github.com/netique/499c0117f092d43980b1c8ea52671499")

来源:

library(shiny)

shinyApp(
  ui = fluidPage(
    selectInput("left", "left",
      choices = LETTERS[1:10],
      multiple = TRUE, selectize = FALSE, size = 10
    ),
    selectInput("right", "right",
      choices = LETTERS[1:10],
      multiple = TRUE, selectize = FALSE, size = 10
    )
  ),
  server = function(input, output) 
    observeEvent(
      input$right,
      
        updateSelectInput(
          inputId = "left",
          choices = setdiff(LETTERS[1:10], isolate(input$right)),
          selected = isolate(input$left)
        )
      
    )
    observeEvent(
      input$left,
      
        updateSelectInput(
          inputId = "right",
          choices = setdiff(LETTERS[1:10], isolate(input$left)),
          selected = isolate(input$right)
        )
      
    )
  
)

【讨论】:

以上是关于Selectizeinput 输入互斥 R Shiny的主要内容,如果未能解决你的问题,请参考以下文章

在我选择并删除一个条目之前,R Shiny 中的 selectizeInput() 不允许输入

R Shiny中的SelectizeInput过滤

R Shiny(selectizeInput):找不到对象“选择”

在带有 R 闪亮的 selectizeInput 中使用 html

赛普拉斯:选择和迭代 R 闪亮的 selectizeInput 元素

R Shiny SelectizeInput:从分组选择中传递值不起作用