6

I would like to display an Rmd file with LaTeX equations in my shiny dashboard app. I've run into problems using includeMarkdown() and includeHTML(). Here is a simplified app of what I'm trying to achieve. Here is my app.R:

library(shinydashboard)
ui <- dashboardPage(
    dashboardHeader(title='My test application'),
    dashboardSidebar(
        sidebarMenu(
            menuItem("Theory", tabName = "theory", icon = icon("book"))
        )
    ),
    dashboardBody(

        tabItems(

            tabItem(tabName="theory",
                    includeMarkdown("Theory.Rmd")
                    #includeMarkdown("Theory.md")
                    #includeHTML("Theory.html")
            )
        )    
    )
)

server <- function(input, output){

}

shinyApp(ui = ui, server = server)

My Theory.Rmd file:

---
title: "Theory"
output: 
  html_document:
    mathjax: "http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"
---
Here is an equation:

$$Q = a(h − c)^b$$ 

Note that in order to run the app, Theory.Rmd and app.R have to be saved in the same directory (e.g. the working directory) hand have to have those exact names. To obtain a markdown Theory.md file of the Rmd file, simply do:

library(knitr)
knit("Theory.Rmd","Theory.md")

And to obtain the Theory.html file, simply press the Knit to HTML button in the Theory.Rmd file

When running my app in my browser or RStudio window, includeMarkdown("Theory.Rmd") or includeMarkdown("Theory.md"), do not render the equations but it starts by default in the theory menu item, like this: enter image description here However using includeHTML("Theory.html") the equations render correctly but the screen display is shortened, and by default it does not start in any menu item, like this:enter image description here

but when clicked on theory I get correctly rendered equations: enter image description here

Is there a way to fix this? Many thanks!

Cyrus Mohammadian
  • 4,982
  • 6
  • 33
  • 62
Sölvi
  • 500
  • 5
  • 17
  • Your app does not run to me, are you sure the example is reproducible? Or maybe I am missing something. – SabDeM Aug 19 '15 at 17:40
  • Theory.Rmd and app.R have to be saved in to the same directory (e.g. the working directory) and have to have those exact names for the app to run. I am pretty sure this is reproducible. Thank you for your time. – Sölvi Aug 19 '15 at 23:46
  • In addition, to run the app, as written in the question you are required to knit the Rmd file to html once to obtain the the Theory.html. The reason for this is that includeHTML('Theory.html') is the one not commented out. I will edit my question and comment includeHTML('Theory.html') out and have includeMarkdown('Theory.Rmd'), so this can be run only by adding app.R and Theory.Rmd files. – Sölvi Aug 19 '15 at 23:57
  • 1
    Note from the future: cdn.mathjax.org is nearing its end-of-life, check https://www.mathjax.org/cdn-shutting-down/ for migration tips. – Peter Krautzberger Apr 12 '17 at 09:06

2 Answers2

7

It is also possible to wrap includeMarkdown() in withMathJax(), so you won't need to change your .md-file:

withMathJax(includeMarkdown("Theory.md"))
michael
  • 150
  • 1
  • 6
0

For people interested this has been solved. The header in the markdown file that was knitted from the Rmd file prevented it from rendering in the Shiny app. Before, the Theory.md file knitted from the Theory.Rmd showed in the question looked like this:

knit("Theory.Rmd","Theory.md")

Theory.md before

---
output: 
 html_document:
 mathjax: "//cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"
---
Here is an equation:

$$Q = a(h − c)^b$$ 

I changed the markdown file manually, I removed the header and added the refrerence to mathjax:

Theory.md after

<script type="text/javascript"
  src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
Here is an equation:

$$Q = a(h − c)^b$$ 

This renders in Shiny when including Markdown in the server like this:

includeMarkdown("Theory.md")
Sölvi
  • 500
  • 5
  • 17