Superscripts in shiny dropdown menu

Is there a simple method for incorporating superscripts/subscripts in shiny selectInput dropdown labels? This seems trivial but I can't find any good posts on it.

Here's an example of what I'm referring to:

# basic example
shinyApp(
  ui = fluidPage(
    selectInput("variable", "Variable:",
                c("Cylinders" = "cyl",
                  "Transmission" = "am",
                  "Gears" = "gear",
                  "Miles gallon<sup>-1</sup>" = "mpg")),
    tableOutput("data")
  ),
  server = function(input, output) {
    output$data <- renderTable({
      mtcars[, c("wt", input$variable), drop = FALSE]
    }, rownames = TRUE)
  }
)

# demoing group support in the `choices` arg
shinyApp(
  ui = fluidPage(
    selectInput("state", "Choose a state:",
                list(`East Coast` = list("NY", "NJ", "CT"),
                     `West Coast` = list("WA", "OR", "CA"),
                     `Midwest` = list("MN", "WI", "IA"))
    ),
    textOutput("result")
  ),
  server = function(input, output) {
    output$result <- renderText({
      paste("You chose", input$state)
    })
  }
)

Thanks!!

this lets your render with HTML in the selection, I'm not sure about the label once its selected. perhaps someone else can contribute that.

library(tidyverse)
library(shiny)

(data_for_select <-     tibble(value=c("cyl","am","gear","mpg"),
                               label=c("cyl","am","gear","mpg"),
                                  html=c("Cylinders","Transmission","Gears" , "Miles gallon<sup>-1</sup>")))


# basic example
shinyApp(
  ui = fluidPage(
    selectizeInput("sel1", label = "Selectize 1", choices = NULL),
    tableOutput("data")
  ),
  server = function(input, output,session) {
    updateSelectizeInput(
      session, "sel1", server = TRUE, 
      choices = data_for_select, 
      selected = data_for_select[[1]][1], 
      
      options = list(render = I(
        '{ option: function(item, escape) {
            return "<div>Select " + item.html + "</div>";
            }
         }'
      ))
    )
    
    output$data <- renderTable({
      mtcars[, c("wt", input$variable), drop = FALSE]
    }, rownames = TRUE)
  }
)

adapted from https://github.com/rstudio/shiny/issues/2172

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.