0
votes

I am trying to update a Shiny selectInput with a list of strings, some of which are quoted. If I use selectize = TRUE, the quoted strings don't appear among the choices, and if use selectize = FALSE, they do appear but cannot be selected. Can someone tell me how to solve this problem? (I have to allow for quotes since both the quoted and unquoted versions of a string may be valid choices.)

My code:

library(shiny)
runApp(list(
  ui=fluidPage(
    titlePanel("Problem with Selecting Quoted String"),
    sidebarLayout(
        sidebarPanel(
            selectInput(
                "chooser",
                label = "Choose",
                choices = "",
                selectize = TRUE
                )
            ),
        mainPanel(
            textOutput("text")
            )
        )
    ),
  server=function(input, output, session) {
  #        choices <- load_choices_from_table()
    choices <- c("\"APPLE\"", "APPLE", "\"BOY\"", "CAT")
    updateSelectInput(session, "chooser", choices = choices)
    output$text <- renderText({
        paste("You chose:", input$chooser)
    })
}
)
)
1
R accepts two kinds of quotes: Have you tried single quotes?IRTFM
My explicit quotes were for illustration only. In reality, the values are coming from a database and there is a chance that some strings are single-quoted as well. Basically, I have to show the strings verbatim.user3434580
Then I think you need to pre-process the strings with gsub(["],"", svec)IRTFM
That sounds like a bug of selectizeInput() in shiny...Yihui Xie

1 Answers

0
votes

I got success with:

choices <- c("\'APPLE\'", "APPLE", "\'BOY\'", "CAT")

And since the default is proportional spacing these loo like double quotes:

choices <- c("\'\'APPLE\'\'", "APPLE", "\'\'BOY\'\'", "CAT")

I tried this after reading some further help pages but it displays the first one but failed to allow selection of that item. I'm starting to wonder if this is a "feature" that allows initial display of a helpful comment that will not be an acceptable choice.

choices <- I( c("\"APPLE\"", "APPLE", "\'\'BOY\'\'", "CAT") )