问题
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:
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:
but when clicked on theory I get correctly rendered equations:
Is there a way to fix this? Many thanks!
回答1:
It is also possible to wrap includeMarkdown()
in withMathJax()
, so you won't need to change your .md
-file:
withMathJax(includeMarkdown("Theory.md"))
回答2:
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")
来源:https://stackoverflow.com/questions/32101134/display-latex-equations-in-a-shiny-dashboard-app-in-r