Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MathJax not working with choices inside selectInput and selectizeInput #1506

Closed
kiendang opened this issue Dec 13, 2016 · 3 comments
Closed

Comments

@kiendang
Copy link

kiendang commented Dec 13, 2016

I want to create a dropdown menu with the choices containing math symbols, but it seems MathJax does not work with labels of choices inside selectInput and selectizeInput.

This is the minimal code using selectizeInput:

library(shiny)

ui <- shinyUI(fluidPage(
  withMathJax(),
  titlePanel("Title"),
  sidebarLayout(
    sidebarPanel(
      selectizeInput('select', label = "Select",
                     choices = list("\\(\\gamma\\)" = 1, "\\(\\beta\\)" = 2))
    ),
    mainPanel()
  )
))

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

shinyApp(ui = ui, server = server)

After the page first loads, MathJax works for the selected item, but not the choices.
screen shot 2016-12-13 at 6 17 27 pm
After another choice other than default is selected, MathJax stops working for the selected item.
screen shot 2016-12-13 at 6 17 33 pm
The same goes for selectInput.

@M-E-Rademaker
Copy link

Have you or anyone found a workaround. Im struggeling with the same...

@bborgesr
Copy link
Contributor

bborgesr commented Oct 6, 2017

There's a way to do this if you use HTML (instead of MathJax) and some JS in the options argument of updateSelectizeInput. Here's a minimal repro:

library(shiny)

greekNames <- list("alpha", "beta", "gamma", "delta", "epsilon")
greekCodes <- list("&#945;", "&#946;", "&#947;", "&#948;", "&#949;")
greek <- setNames(greekNames, greekCodes)

ui <- fluidPage(
  selectizeInput("select", label = "Select", choices = NULL),
  textOutput("txt")
)

server <- function(input, output, session) {
  
  updateSelectizeInput(
    session, "select",
    choices = greek,
    options = list(render = I("
      {
        item:   function(item, escape) { return '<div>' + item.label + '</div>'; },
        option: function(item, escape) { return '<div>' + item.label + '</div>'; }
      }
    "))
  )
  
  output$txt <- renderText({
    paste("You chose", input$select)
  })
}

shinyApp(ui, server)

FYI, if you don't mind using checkboxGroupInput or radioButtons instead of the selectizeInput, there's an even easier way to do this (using the relatively new choiceNames and choiceValues arguments):

library(shiny)

greekNames <- list("alpha", "beta", "gamma", "delta", "epsilon")
greekCodes <- list("&#945;", "&#946;", "&#947;", "&#948;", "&#949;")
greekHtml <- lapply(greekCodes, HTML)

ui <- fluidPage(
  checkboxGroupInput("box", "Choose one or more greek letters",
    choiceNames = greekHtml,
    choiceValues = greekNames
  ),
  radioButtons("radio", "Choose one greek letter",
    choiceNames = greekHtml,
    choiceValues = greekNames
  ),
  textOutput("selected_boxes"),
  textOutput("selected_buttons")
)

server <- function(input, output, session) {
  output$selected_boxes <- renderText({
    paste("Checkboxes: you chose", paste(input$box, collapse = ", "))
  })
  
  output$selected_buttons <- renderText({
    paste("Radio buttons: you chose", input$radio)
  })
}

shinyApp(ui, server)

@stla
Copy link

stla commented Jul 9, 2018

FYI, the math rendering works fine with katex.

library(shiny)

greekNames <- list("alpha", "beta")
greekCodes <- list("\\\\\\alpha", "\\\\\\beta") # note the six backslahes
greek <- setNames(greekNames, greekCodes)

ui <- fluidPage(
  tags$head(
    tags$link(rel="stylesheet", href="https://cdn.jsdelivr.net/npm/katex@0.10.0-beta/dist/katex.min.css", integrity="sha384-9tPv11A+glH/on/wEu99NVwDPwkMQESOocs/ZGXPoIiLE8MU/qkqUcZ3zzL+6DuH", crossorigin="anonymous"),
    tags$script(src="https://cdn.jsdelivr.net/npm/katex@0.10.0-beta/dist/katex.min.js", integrity="sha384-U8Vrjwb8fuHMt6ewaCy8uqeUXv4oitYACKdB0VziCerzt011iQ/0TqlSlv8MReCm", crossorigin="anonymous")
  ),
  selectizeInput("select", label = "Select", choices = NULL),
  textOutput("txt")
)

server <- function(input, output, session) {
  
  updateSelectizeInput(
    session, "select",
    choices = greek,
    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>'; 
    }
    }
                              "))
    )
  
  output$txt <- renderText({
    paste("You chose", input$select)
  })
  }

shinyApp(ui, server)

screenshot 6

@kiendang kiendang closed this as completed Jul 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants