library(flextable)
flextable::set_flextable_defaults(digits = 2)
You will need package 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
β0 | β1 | Xi | p |
-1 | 1 | 2 | 0.383 |
-1 | 1 | 1 | 0.760 |
-1 | 1 | 0 | 0.400 |
-1 | 1 | -1 | 0.314 |
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()
β0 | β1 | Xi | p |
-1 | 1 | 2 | 0.932 |
-1 | 1 | 1 | 0.077 |
-1 | 1 | 0 | 0.468 |
-1 | 1 | -1 | 0.508 |