长的selectInput列表中有R个闪亮的收藏夹

时间:2019-06-16 01:07:47

标签: r shiny

我想知道如何处理一长串的选项。例如,在下面的示例中,我有一个选项的子集作为收藏夹,但希望能够选择所有选项,包括非收藏夹。如何获得基于radiogroupbutton()和selectInput()的input $ selected返回我最后选择的内容? 干杯, 卢克

编辑:我想保持外观,它具有单选按钮和一个下拉列表。因此,我假设两者都需要不同的inputID,然后可以在服务器站点中将它们组合(以某种方式)(如Joris所建议的)。我不确定如何在服务器站点上将它们组合在一起,以及如何确定最后选择的内容。

ALL.options <- apply(expand.grid(LETTERS, LETTERS), 1, function(x){paste(x, collapse="")})
favourites <- sample(ALL.options, 20)




ui <- fluidPage(

  h3("Favourites:"),
      radioGroupButtons(inputId="selected", 
                        choices = sort(favourites), 
                        individual = TRUE, 
                        selected = NULL, 
                        width="20%"),

  selectInput(inputId="selected", label = "Other options",
              choices = ALL.options, 
              selected = NULL),

    h3("THIS IS YOUR SELECTION:"),

  verbatimTextOutput("choice")

)

)

server <- function(input, output) {

output$choice <- renderPrint(
  input$selected
  )

}

shinyApp(ui, server)

2 个答案:

答案 0 :(得分:2)

使用单个selectInputselectizeInput足以在单独的选项组中列出收藏夹其他选项(例如,参见{ {3}}):

library(shiny)

ALL.options <- apply(expand.grid(LETTERS, LETTERS), 1, function(x){paste(x, collapse="")})
favourites <- sample(ALL.options, 20)


ui <- fluidPage(

    selectizeInput(inputId = "selected", label = "All options", choices = list(
            Favourites = favourites,
            `Other options` = setdiff(ALL.options, favourites)
        ), 
        options = list(
            placeholder = '<None selected>',
            onInitialize = I('function() { this.setValue(""); }')
        )
    ),

    h3("THIS IS YOUR SELECTION:"),

    verbatimTextOutput("choice")

)

server <- function(input, output) {

  output$choice <- renderPrint({

        validate(need(input$selected, "None selected"))

        input$selected

      })

}

shinyApp(ui, server)

注意:如果您改为使用两个单独的输入(radioGroupButtonsselectInput),则可以在服务器端将选择的选项组合在反应对象中。例如:

library(shiny)
library(shinyWidgets)

ALL.options <- apply(expand.grid(LETTERS, LETTERS), 1, function(x){paste(x, collapse="")})
favourites <- sample(ALL.options, 20)

ui <- fluidPage(

    h3("Favourites:"),
    radioGroupButtons(inputId = "radio", 
        choices = sort(favourites), 
        individual = TRUE, 
        selected = character(0), 
        width="20%"),

    selectizeInput(inputId="select", label = "Other options",
        choices = ALL.options,
        options = list(
            placeholder = '<None selected>',
            onInitialize = I('function() { this.setValue(""); }')
        )
    ),  

    h3("THIS IS YOUR SELECTION:"),

    verbatimTextOutput("choice")

)

server <- function(input, output) {

  ## initialize reactive value
  currentSelected <- reactiveVal(NULL)

  ## update based on radioGroupButtons
  observeEvent(input$radio, {

        currentSelected(input$radio)

      })

  ## update based on selectInput
  observeEvent(input$select, {

        currentSelected(input$select)

      })

  output$choice <- renderPrint({

        validate(need(currentSelected(), "None selected"))

        currentSelected()

      })

}

shinyApp(ui, server)

Shiny: Option groups for selectize input(v0.3.0)于2019-06-17创建

答案 1 :(得分:0)

我不确定我是否完全了解您要达到的目标。我还注意到--- Original --- # dummy text 1 webApplication.ChildBucketOne # dummy text 2 webApplication.ChildBucketTwo # dummy text 3 --- Replaced --- # dummy text 1 webApplication.ParentBucket.ChildBucketOne # dummy text 2 webApplication.ParentBucket.ChildBucketTwo # dummy text 3 radioGroupButtons都具有相同的selectInput。如果要打印两个选项,则可以将inputId的{​​{1}}更改为inputId,然后将selectInput修改为:

select

这是您要寻找的吗?