rshinylatexreactiveuioutput

withMathJax format lost after reactive input changed


Example Code:

ui <- fluidPage(

  withMathJax(),
  tags$div(HTML("<script type='text/x-mathjax-config'>
                MathJax.Hub.Config({
                tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}
                });
                </script>
                ")),

    sidebarPanel(sliderInput("k", "(k_{test})", value=2, min=1, max=3)),

    mainPanel(uiOutput("out"))
)

server <- function(input, output, session){
  output$out <- renderUI({ paste("(k_{test}=)", input$k)}) 
}

shinyApp(ui, server)

Here is my Problem: Initially, the app works fine. When changing the input on the slider, however, the output equation loses its format and I don't know how to fix this.

Thanks in advance!


Solution

  • ?withMathJax
    

    says:

    It only needs to be called once in an app unless the content is rendered after the page is loaded, e.g. via renderUI, in which case we have to call it explicitly every time we write math expressions to the output.

    output$out <- renderUI({ 
      list(
        withMathJax(),
        paste("(k_{test}=)", input$k)
      )
    }) 
    

    should solve your problem