Permalink
Browse files

remove includeMathJax() and use withMathJax() instead, per suggestion…

… of @jcheng5
  • Loading branch information...
1 parent 2b2ed81 commit 5c34aa0bb5f69a7d3df8ba5cd9eb376f1419329b @yihui yihui committed Feb 18, 2014
Showing with 43 additions and 10 deletions.
  1. +1 −1 NAMESPACE
  2. +1 −1 NEWS
  3. +18 −5 R/shinyui.R
  4. +0 −3 man/include.Rd
  5. +23 −0 man/withMathJax.Rd
View
2 NAMESPACE
@@ -69,7 +69,6 @@ export(img)
export(includeCSS)
export(includeHTML)
export(includeMarkdown)
-export(includeMathJax)
export(includeScript)
export(includeText)
export(installExprFunction)
@@ -158,6 +157,7 @@ export(validateCssUnit)
export(verbatimTextOutput)
export(verticalLayout)
export(wellPanel)
+export(withMathJax)
export(withTags)
import(RJSONIO)
import(caTools)
View
2 NEWS
@@ -57,7 +57,7 @@ shiny 0.8.0.99
reactive one (i.e. reading the variable makes the current reactive context
dependent on it, and setting the variable is a source of reactivity).
-* Added a function `includeMathjax()` to include the MathJax library in an app.
+* Added a function `withMathJax()` to include the MathJax library in an app.
* The argument `selected` in checkboxGroupInput(), selectInput(), and
radioButtons() refers to the value(s) instead of the name(s) of the
View
23 R/shinyui.R
@@ -138,12 +138,25 @@ includeScript <- function(path, ...) {
return(tags$script(HTML(paste(lines, collapse='\r\n')), ...))
}
-#' @rdname include
+#' Load the MathJax library and typeset math expressions
+#'
+#' This function adds MathJax to the page and typeset the math expressions (if
+#' found) in the content \code{...}. It only needs to be called once in an app
+#' unless the content is rendered \emph{after} the page is loaded, e.g. via
+#' \code{\link{renderUI}}, in which case we have to call it explicitly every
+#' time we write math expressions to the output.
+#' @param ... any HTML elements to apply MathJax to
#' @export
-includeMathJax <- function(path) {
- if (missing(path))
- path <- 'https://c328740.ssl.cf1.rackcdn.com/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'
- tags$head(singleton(tags$script(src = path, type = 'text/javascript')))
+#' @examples withMathJax(helpText("Some math here $$\\alpha+\\beta$$"))
+#' # now we can just write "static" content without withMathJax()
+#' div("more math here $$\\sqrt{2}$$")
+withMathJax <- function(...) {
+ path <- 'https://c328740.ssl.cf1.rackcdn.com/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'
+ tagList(
+ tags$head(singleton(tags$script(src = path, type = 'text/javascript'))),
+ ...,
+ tags$script(HTML('MathJax.Hub.Typeset();'))
+ )
}
#' Include Content Only Once
View
3 man/include.Rd
@@ -4,7 +4,6 @@
\alias{includeCSS}
\alias{includeHTML}
\alias{includeMarkdown}
-\alias{includeMathJax}
\alias{includeScript}
\alias{includeText}
\title{Include Content From a File}
@@ -18,8 +17,6 @@ includeMarkdown(path)
includeCSS(path, ...)
includeScript(path, ...)
-
-includeMathJax(path)
}
\arguments{
\item{path}{The path of the file to be included. It is
View
23 man/withMathJax.Rd
@@ -0,0 +1,23 @@
+% Generated by roxygen2 (4.0.0): do not edit by hand
+\name{withMathJax}
+\alias{withMathJax}
+\title{Load the MathJax library and typeset math expressions}
+\usage{
+withMathJax(...)
+}
+\arguments{
+ \item{...}{any HTML elements to apply MathJax to}
+}
+\description{
+This function adds MathJax to the page and typeset the math expressions (if
+found) in the content \code{...}. It only needs to be called once in an app
+unless the content is rendered \emph{after} the page is loaded, e.g. via
+\code{\link{renderUI}}, in which case we have to call it explicitly every
+time we write math expressions to the output.
+}
+\examples{
+withMathJax(helpText("Some math here $$\\\\alpha+\\\\beta$$"))
+# now we can just write "static" content without withMathJax()
+div("more math here $$\\\\sqrt{2}$$")
+}
+

2 comments on commit 5c34aa0

@jcheng5
RStudio member

I seem to get a JS error on the first call to typeset here:

https://gist.github.com/jcheng5/999d2c0d5a6fe48ff137

@yihui
RStudio member

I feel we may need two separate functions includeMathJax() and withMathJax() -- the former is for the whole page and must be called outside renderUI(); the latter is for more slippery cases like renderUI(), i.e. when it is too late for MathJax to detect math expressions in the page.

withMathJax <- function(...) {
  tagList(
    includeMathJax(),
    ...,
    tags$script(HTML('MathJax.Hub.Typeset();'))
  )
}
includeMathJax <- function() {
  path <- 'https://c328740.ssl.cf1.rackcdn.com/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'
  tags$head(singleton(tags$script(src = path, type = 'text/javascript')))
}

We can certainly also keep the current version of withMathJax(), but make sure to call it early. For your example, this will work:

shinyUI(basicPage(
  withMathJax(),
  uiOutput('foo')
))
Please sign in to comment.