Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display R formula elegantly (as in Latex)

Tags:

r

I want to display an R equation in a legible format, to see the formula.

Let's say I have :

A=B/C

I want to know if there is some way to display it like it would be shown in latex output:

$A=\frac{B}{C}$
like image 351
Ghost Avatar asked Jul 02 '15 20:07

Ghost


1 Answers

It is quite possible that this already exists somewhere, but you can write a function to render a formula with mathjax and display it in the viewer (if you are using rstudio) or in your web browser. This would help if you wanted to edit/check formulas on the fly I guess

form1 <- '$$A=\\frac{B}{C}$$'
form2 <- '$$
  \\frac{1}{\\displaystyle 1+
      \\frac{1}{\\displaystyle 2+
          \\frac{1}{\\displaystyle 3+x}}} +
  \\frac{1}{1+\\frac{1}{2+\\frac{1}{3+x}}}
$$'
form3 <- '\\frac{d}{dx}\\left( \\int_{0}^{x} f(u)\\,du\\right)=f(x)'

show_math(form1)

enter image description here

Or open in your browser if you don't use rstudio

show_math(form2, use_viewer = FALSE) ## opens in default browser

enter image description here

show_math(form1, form2, form3, css = 'color: red; font-size: 15px;')

enter image description here

I'm pretty sure mathjax doesn't have full support of latex equations. And be sure to escape the \ in the formula

You can also display more than a single line which is cool.

form4 <- "
\\forall a,b,c \\in \\mathbb{R} \\\\
\\begin{align}
                      a + b &= c \\\\
             (a + b)(a - b) &= c(a - b) \\\\
                  a^2 - b^2 &= ca - cb \\\\
                   a^2 - ca &= b^2 - cb \\\\
  a^2 - ca + \\frac{c^2}{4} &= b^2 - cb + \\frac{c^2}{4} \\\\
       (a - \\frac{c}{2})^2 &= (b - \\frac{c}{2})^2 \\\\
           a - \\frac{c}{2} &= b - \\frac{c}{2} \\\\
                          a &= b \\qquad \\qquad \\blacksquare \\\\
 \\end{align}
"

show_math(form4)

enter image description here

show_math <- function(..., css = '', use_viewer = !is.null(getOption('viewer'))) {
  mj <- "<script>
  (function () {
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src  = 'https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML';
    document.getElementsByTagName('head')[0].appendChild(script);
  })();
</script>"
  
  ## view text strings as html in viewer/browser
  view_html <- function(..., viewer) {
    x <- c(...)
    if (is.null(x)) return(invisible())
    htmlFile <- tempfile(fileext = '.html')
    writeLines(x, con = htmlFile)
    if (viewer)
          tryCatch(rstudio::viewer(htmlFile),
                   error = function(e) {
                     message('Viewer not available - opening in browser.\n',
                             'If using Rstudio, try installing the \'rstudio\' package.',
                             domain = NA)
                     browseURL(htmlFile)
                   })
     else browseURL(htmlFile)
     invisible(x)
  }
  
  ## use \[ expr \] instead of $$ expr $$
  check_expr <- function(x)
    sprintf('\\[%s\\]', gsub('^\\$+|\\$+$', '', x))
  
  x <- paste(sapply(c(...), check_expr), collapse = '<br />')
  if (!nzchar(x))
    return(invisible(NULL))

  ## setting the default to larger font since @Molx has bad eyes :}
  ## this can still be over-ridden by passing font-size: whatever; to css

  view_html(sprintf('<span class="math" style="font-size: 24px; %s;">\n', css),
            x, '\n</span>\n', mj, viewer = use_viewer)
}
like image 145
rawr Avatar answered Oct 23 '22 15:10

rawr