Search code examples
rshinyconditional-statementsmathjax

R shiny with mathjax conditional cases


Hi I am aware that you can have inline equations in an R-shiny app using the withMathJax function, but from what I have seen it is simple math equations that you can use like at the Rshiny examples here. I was wondering if any one new how to add conditional cases, as you would in latex with the following code

f(n) =
\begin{cases}
n/2,  & \text{if $n$ is even} \\
3n+1, & \text{if $n$ is odd}
\end{cases}

which would produce the below, cheers

conditional output


Solution

  • I think thats what you want or?

    library(shiny)
    
    ui <- {fluidPage(
      title = 'MathJax Examples',
      withMathJax(),
      uiOutput('ex4')
      )}
    
    server <- function(input, output, session) {
      output$ex4 <- renderUI({
        withMathJax(
          helpText('$$f(n)=\\begin{cases}
                   n/2,  & \\text{if $n$ is even} \\\\
                   3n+1, & \\text{if $n$ is odd}
                   \\end{cases}\\!$$'))
      })
    }
    shinyApp(ui, server)