stats_curious
stats_curious

Reputation: 21

latex in shiny selectInput

I'm wondering if someone knows how to include LaTeX code inside of a Shiny selectInput() command. I'd like to do something like:

ui <- shinyUI(
  fluidPage(
       selectInput(
        c("$\mu_0 \leq 3$" = "less",
          "$\mu_0 \geq 3" = "more",
          "$\mu_0 = 3" = "equal"
        )
    )
)

server <- function(input, output, session){  
      # server side code here...
}

shinyApp(ui = ui, server = server)

Upvotes: 2

Views: 388

Answers (2)

St&#233;phane Laurent
St&#233;phane Laurent

Reputation: 84649

You can use katex.

library(shiny)

choices <- c("less", "more", "equal")
choicesNames <- c("\\\\\\mu_0 \\leq 3", 
                  "\\\\\\mu_0 \\geq 3",
                  "\\\\\\mu_0 = 3")
choices <- setNames(choices, choicesNames)

ui <- shinyUI(
  fluidPage(
    tags$head(
      tags$link(rel="stylesheet", 
                href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css", integrity="sha384-9tPv11A+glH/on/wEu99NVwDPwkMQESOocs/ZGXPoIiLE8MU/qkqUcZ3zzL+6DuH", 
                crossorigin="anonymous"),
      tags$script(src="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.js", integrity="sha384-U8Vrjwb8fuHMt6ewaCy8uqeUXv4oitYACKdB0VziCerzt011iQ/0TqlSlv8MReCm", 
                  crossorigin="anonymous")
    ),
    selectizeInput(
      "ID", 
      "LABEL", 
      choices,
      options = list(render = I("
    {
      item: function(item, escape) { 
              var html = katex.renderToString(item.label);
              return '<div>' + html + '</div>'; 
            },
      option: function(item, escape) { 
                var html = katex.renderToString(item.label);
                return '<div>' + html + '</div>'; 
              }
    }"))
    )
  )
)

server <- function(input, output, session){  
    # server side code here...
}

shinyApp(ui = ui, server = server)

enter image description here

Upvotes: 3

Antarqui
Antarqui

Reputation: 497

Would something like this help you?

selectInput("mean", label = "Parameter", choices=c("\u03BC"="mu"))

Upvotes: 0

Related Questions