Force no default selection in selectInput()

前端 未结 6 1152
别跟我提以往
别跟我提以往 2021-02-01 02:17

The Shiny documentation mentions that for selectInput():

selected The value (or, if none was supplied, the title) of the naviga

相关标签:
6条回答
  • 2021-02-01 02:35

    Answering after 6 years, as I got the same error now. We can write selected = character(0) to force not selecting any user input.

    0 讨论(0)
  • 2021-02-01 02:39

    @Yihui Xie's answer requires selectInput(..., selectize = TRUE). If you want selectize = FALSE, you can still achieve a similar effect as follows.

    This is not documented:

    selected The initially selected value (or multiple values if multiple = TRUE). If not specified then defaults to the first value for single-select lists and no values for multiple select lists.

    But for single-select lists if you can use selectize = FALSE, size = 4 (any non-NULL size would work), then you can set selected = FALSE to force no default selection.

    library(shiny)
    
    # Define UI for application that draws a histogram
    ui <- fluidPage(
          mainPanel(
             uiOutput("Choose_Molecule")
          )
    )
    
    # Define server logic required to draw a histogram
    server <- function(input, output) {
    
        # First UI input (Service column) filter clientData 
        output$Choose_Molecule <- renderUI({
            selectInput("molecule",
                        "Select Molecule:",
                        choices = rownames(mtcars),
                        selected = FALSE,
                        multiple = FALSE
                        , selectize = FALSE, size = 4  ##needed for `selected = FALSE` to work
            )
        })
    
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    

    0 讨论(0)
  • 2021-02-01 02:41

    I think you can get around it by adding an empty string to your choice list:

    selectInput("app", 
    "Select App:", 
    choices = c("", as.character(mtrl_name)), 
    selected = NULL, 
    multiple = FALSE)
    
    0 讨论(0)
  • 2021-02-01 02:44

    Faced a similar issue. The solution I found is based of @MKa's answer. You do not want to set multiple=T if your code can't handle multiple values. What I suggest is:

    selectInput("molecule",
                "Select Molecule:",
                choices = c("",as.character(mtrl_name)),
                selected = NULL,
                multiple = F
               ) 
    

    And to retrieve the value selected:

    if(!is.null(input$molecule))
    {
      if(nchar(input$molecule)>1)
      {
        #do your thing..
      }
    }
    

    Fixed my problem. Let me know if you found a better solution.

    0 讨论(0)
  • 2021-02-01 02:49

    A workaround would be to use updateSelectInput. The point here is that you set your selected = argument to length(your_vector_with_choices).

    You can create a reactiveValues if you want your selectInput to be updated at the very beginning (in that case only observed at program start).

    library(shiny)
    
    shinyApp(
      ui = fluidPage(
        selectInput("state", "Choose a state:", NULL),
        textOutput("result")
      ),
      
      server = function(input, output, session) {
        values <- reactiveValues(start = NULL)
        
        observe({
          choiceList <- list(`East Coast` = list("NY", "NJ", "CT"),
                             `West Coast` = list("WA", "OR", "CA"),
                             `Midwest` = list("MN", "WI", "IA"))
          updateSelectInput(session, "state", choices = choiceList, selected = choiceList[length(choiceList)])
        })
        
        output$result <- renderText({
          paste("You chose", input$state)
        })
      }
    )
    
    0 讨论(0)
  • 2021-02-01 02:50

    You can use the selectize input instead of the select input, with some custom selectize options to set the initial selection to be empty. An example has been provided in the Shiny Gallery. In particular, see the example #6.

    # make sure you have shiny >= 0.9.1
    selectizeInput(
      'e6', '6. Placeholder', choices = state.name,
      options = list(
        placeholder = 'Please select an option below',
        onInitialize = I('function() { this.setValue(""); }')
      )
    )
    

    BTW, for the error "ERROR: bad 'file' argument", I do not think anybody can help you without seeing the source code of your app, and it may be a separate question.

    0 讨论(0)
提交回复
热议问题