13

I want to display a -LaTeX formated- formula in a Shiny panel, but I can't find a way to combine textOutput with withMathJax. I tried the following but it didn't work. Any help would be gratefully appreciated.

--ui.r

...
    tabPanel("Diagnostics", h4(textOutput("diagTitle")),
withMathJax(textOutput("formula")),
),
...

--server.r

...
output$formula <- renderText({
    print(paste0("Use this formula: $$\\hat{A}_{\\small{\\textrm{M€}}} =", my_calculated_value,"$$"))
})
...
gd047
  • 29,749
  • 18
  • 107
  • 146
  • strange, it run fines as is on my machine (I just removed the `print` but shouldn't change much). I have shiny_0.11.1 though. Is this script file: ` – NicE May 14 '15 at 16:24
  • Yes, In page source I saw it's loaded. src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" type="text/javascript" – gd047 May 15 '15 at 06:20
  • http://shiny.rstudio.com/gallery/mathjax.html http://sas-and-r.blogspot.gr/2015/12/write-in-line-equations-in-your-shiny.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed:+SASandR+(SAS+and+R) – gd047 Dec 31 '15 at 09:21

3 Answers3

9

Use uiOutput on the UI side and renderUI on the server side for dynamic content.

ui <- fluidPage(
  withMathJax(),
  tabPanel(
    title = "Diagnostics", 
    h4(textOutput("diagTitle")),
    uiOutput("formula")
  )
)

server <- function(input, output, session){
  output$formula <- renderUI({
    my_calculated_value <- 5
    withMathJax(paste0("Use this formula: $$\\hat{A}_{\\small{\\textrm{M€}}} =", my_calculated_value,"$$"))
  })
}

shinyApp(ui, server)

More examples: http://shiny.leg.ufpr.br/daniel/019-mathjax/

GyD
  • 3,902
  • 2
  • 18
  • 28
3

ui.R

tabPanel("Diagnostics", h4(textOutput("diagTitle")),
    withMathJax(uiOutput("formula")),
)

server.R

output$formula <- renderUI({
    return(HTML(paste0("<p>", "Use this formula: $$\\hat{A}_{\\small{\\textrm{M€}}} =", my_calculated_value,"$$","</p>")))
})
madx
  • 6,723
  • 4
  • 55
  • 59
Quinn Weber
  • 927
  • 5
  • 10
  • 2
    The output is still like that: Use this formula: $$\hat{A}_{\small{\textrm{M€}}} =1.69$$ – gd047 May 14 '15 at 06:12
0

How about using renderPrint()?

Minimal working example:

library(shiny)

server <- function(input, output, session) {

 output$formula <- renderPrint({
     print(paste0("Use this formula: $$\\hat{A}_{\\small{\\textrm{M€}}} =", 1,"$$"))
})

}

ui <- fluidPage(


  titlePanel("Hello Shiny!"),


  sidebarLayout(
    sidebarPanel(

    ),

    mainPanel(
      withMathJax(textOutput("formula"))
    )
    )
)

shinyApp(ui = ui, server = server)

EDIT: To me it looks like this: enter image description here

Mikael Jumppanen
  • 2,436
  • 1
  • 17
  • 29