Search code examples
rshinyshinybs

Mathjax in a shinyBS alert


Is there a way to use MathJax inside an alert created by shinyBS? The attemp below does not work as expected.

library(shiny)
library(shinyBS)
shinyApp(
  ui =
    fluidPage(
      sidebarLayout(
        sidebarPanel(textInput("num1", NULL, value = 100),
                     "divided by", textInput("num2", NULL, value = 20),
                     "equals", textOutput("exampleOutput")),
        mainPanel(
          withMathJax(bsAlert("alert"))
        )
      )
    ),
  server =
    function(input, output, session) {
      output$exampleOutput <- renderText({
        num1 <- as.numeric(input$num1)
        num2 <- as.numeric(input$num2)

        if(num2 == 0) {
          createAlert(session, "alert", "exampleAlert", title = "Oops",
                      content = HTML("You cannot divide by <span class='math inline'>\\(0\\)</span>."))
        } else {
          closeAlert(session, "exampleAlert")
          return(num1/num2)
        }

      })
    }
)

Solution

  • This works with withMathJax(bsAlert("alert")) as in the OP and

    content = "You cannot divide by <span class='math inline'>\\(0\\)</span>.<script>if (window.MathJax) MathJax.Hub.Queue(['Typeset', MathJax.Hub]);</script>"
    

    Strange because the script is included two times.