Columns names with MathJax

cell-content compose

This example shows how to create a flextable equations in header. It is created with a data.frame whose column names contain LaTeX equations.


Packages

library(flextable)
library(magrittr)
flextable::set_flextable_defaults(digits = 2)

Package equatags

You will need package equatags. This package is based on “node.js”, you need to run once equatags::mathjax_install().

if(!equatags::mathjax_available()){
  equatags::mathjax_install()
}

A solution with equatags

ft <- data.frame(
  `\\beta_0` = rep(-1, 4),
  `\\beta_1` = rep(1, 4),
  `X_i` = c(2, 1, 0, -1),
  `p` = runif(4), check.names = FALSE) %>% 
  flextable() %>% 
  colformat_double(j = 4, digits = 3) %>% 
  mk_par(i = 1, part = "header",
         value = as_paragraph(
           as_equation(.,width = .1, height = .2)),
         use_dot = TRUE) %>% 
  autofit()
ft

A solution without equatags

If you can’t use “equatags” because of the “node.js” dependency…

This solution is only viable when hard-coding the values and is not suitable for automation needs.

data.frame(
  a = rep(-1, 4),
  b = rep(1, 4),
  c = c(2, 1, 0, -1),
  d = runif(4), check.names = FALSE) %>% 
  flextable() %>% 
  colformat_double(j = 4, digits = 3) %>% 
  mk_par(j = 1, part = "header", value = as_paragraph(as_i("β"), as_sub("0"))) %>% 
  mk_par(j = 2, part = "header", value = as_paragraph(as_i("β"), as_sub("1"))) %>% 
  mk_par(j = 3, part = "header", value = as_paragraph(as_i("X"), as_sub("i"))) %>% 
  mk_par(j = 4, part = "header", value = as_paragraph(as_i("p"))) %>% 
  autofit()
Copyright © ArData 2021  -  Template by Bootstrapious.com & ported to Hugo by Kishan B