Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one use special symbols in a choices list as names?

Tags:

r

shiny

How to use Greek letters in ui.R has been discussed here before. As an example if one wants the user to input a numeric value for a parameter mu one can use:

numericInput("mu",HTML("μ"),value=0)  

and μ is displayed. I want to do the same thing but with the choices list in a selectInput. I have tried:

selectInput("param", label = "Parameter",choices=c("α" = "alpha","beta"))

but this just displays α. I also tried:

selectInput("param", label = "Parameter",choices=c(HTML("α") = "alpha","beta"))

and

selectInput("param", label = "Parameter",choices=c("HTML("α")" = "alpha","beta"))

but these produce errors.

like image 751
Wolfgang Rolke Avatar asked Aug 19 '16 13:08

Wolfgang Rolke


2 Answers

Don't use the HTML Greek letters, but the \uXXXX version like so:

selectInput("param", label = "Parameter",choices=c("\u03B1" = "alpha",
                                                     "\u03B2" = "beta",
                                                     "\u03BC"="mu"))

Here is a table of \uXXXX for Greek letters: http://www.javascripter.net/faq/greekletters.htm

edit: OP is correct that this won't work for Chi and many other letters for some strange reason, but if you create the named vector for choices in an earlier step it works like so:

choices <- c("alpha","beta","mu","chi")
names(choices) <- c("\u03B1","\u03B2","\u03BC","\u03C7")
selectInput("param","Parameters",choices=choices)

edit2: Things continue to be weird with R and unicode, but I got pretty close to what you want. I can't find a working unicode in R for subscript k, but I did for subscript i so if you are willing to use a different subscript:

library(shiny)      
choices <- c("X^2_i")
names(choices) <- c("\u03C7\u1d62\U00B2")
runApp(
  list(
    ui = fluidPage(
      selectInput("param","Parameters",choices=choices)
    ),
    server = function(input, output, session) {

    }
  )
)

edit3: I still think Unicode is the best way to go since including HTML in select options is not trivial apparently: How do I dynamically create an <option> in JavaScript that contains an HTML entity (— ... «)?

I was able to get the HTML to render in the drop down, but not for the selected item.

library(shiny)
ui <- shinyUI(fluidPage(
  mainPanel(
    selectizeInput("test","test",choices=c("&chi;<sup>2</sup><sub>k</sub>"="chi_k^2"),options=list(
      labelField="name",
      create=FALSE,
      render=I(
        "{option:function(item,escape) {
          return item.name}}"
      )
    )
    )
  )
)
)

server <- shinyServer(function(input, output) {
})

shinyApp(ui = ui, server = server)

There may be a way to tell selectizejs to render html in the selected box, but I haven't seen it anywhere so I remain skeptical.

like image 63
Carl Avatar answered Nov 17 '22 09:11

Carl


Looking at the answer by Carl, and looking into the selectizejs Readme There is a list of fields that can be set for render. Using "option" (the items in the list of all items) and "item" (the items that are selected) I was able to come up with the following solution that will properly render the selected input as well as those in the dropdown list:

library(shiny)
ui <- shinyUI(fluidPage(
  mainPanel(
    selectizeInput("test","test",choices=c("&chi;<sup>2</sup><sub>k</sub>"="chi_k^2"),options=list(
      labelField="name",
      create=FALSE,
      render=I(
        '
        {
            option:function(item,escape) {return String.prototype.concat("<div>", item.name, "</div>"},
            item:function(item,escape) {return String.prototype.concat("<div>", item.name, "</div>"}
        }
        '
      )
    )
    )
  )
)
)

server <- shinyServer(function(input, output) {
})

shinyApp(ui = ui, server = server)
like image 42
Fraser Avatar answered Nov 17 '22 09:11

Fraser