简体   繁体   中英

Empty chain not shown as a choice in selectInput in shiny App

I want the user of the App to select the text wrappers for some files they must upload. The options are No Wrapper, Double Quotes and Simple Quotes, so I want the selectInput function to return "" , "\\"" or "'" , but the first choice does not appear. I show a dummy example:

ui.R

shinyUI(fluidPage(
  selectInput("quotes","Select Text wrapper", 
              choices= list("None"="","Double Quotes (\")"='"', "Simple Quotes (\')"="'"), 
              selected="'"),
  verbatimTextOutput("value")
))

server.R

shinyServer(function(input,output){
  output$value <- renderPrint({ input$quotes })
})

How to make the None choice to appear and to assign an empty character value "" to input$quotes ?

Note: I know that it works with selectize=FALSE , but I would like to keep the looks of the selectize environment. I also know that you can delete the value and click in the box to get the empty value, but I think this isn't intuitive for the user, I want to have an explicit 'None' choice.

Thanks in advance!

UPDATE: In looking at the code changes made to selectize.js to allow options with no values ot be used as placeholder text, ( here ) it appears that the option with no value takes precedence over an explicit "placeholder" tag. Thus there seems to be no way with the current version of the library (0.10.1) to do this.

So it looks like the selectize.js folks decided that any options with a zero-length string as a value will be used as the placeholder text and not be listed in the input. (See this ). This is consistent with the html5 spec for that element. The alternative is that if you set a "placeholder" attribute for the element, it will use that instead. I didn't see a super simple way to set such an element for the select element. I ended up writing this code

setPlace <- function(t, p) {
    setattr <- function(x, name, nameval, attr, val) {
        if (!is.null(names(x)) && name %in% names(x) && x[[name]] == nameval) {
            x[[c("attribs",attr)]] <- val
        } else {
            if(is.list(x)) {
                oa <- attributes(x)
                x <- lapply(x, function(z) setattr(z,name, nameval, attr, val))
                attributes(x)<-oa
            }
        }
        x
    }
    setattr(t, "name", "select", "placeholder",HTML(p))
}

which will take the HTML returned by selectInput and add the attribute into the element. Then you could use it like

setPlace(selectInput("quotes","Select Text wrapper", 
    choices= list("None"="","Double Quotes (\")"='"', "Simple Quotes (\')"="'"), 
    selected="'"), "choose wisely")

or something like that in your layout. This will set the placeholder attribute which (since you set a selected value) probably will never be seen. That seemed to work in my tests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM