Let's say I have the following rmd:
---
title: "Table won't work"
author: "Exhausted student"
date: "2022/01/28"
output:
bookdown::word_document2
---
```{r table, echo=F, warning=F, message=F}
library(tidyverse)
a <- tibble(
constants = c("c", "NA", "h", "e", "H2O"),
values = c(2.998e8, 6.022e23, 6.626e-34, -1.602e-19, 18.02)
)
knitr::kable(a, digits = 35)
```
which produces this table in Word.
The Problem
I need the scientific format to use superscripts and multiply sign (i.e. 2.998 × 108), and some cells requires subscript (e.g. NA and H2O).
The final table should look like this. How can I do that?
What I've tried/would never try
huxtable
package and itsmarkdown()
function: I managed to format some contents asH~2~O
, then enable markdown across table byhuxtable(a) %>% `markdown<-`(TRUE)
. Which did not recognize the syntax, and apparently would not work in forseeable future according to the author.flextable
andas_sub()
: Produces right format. I pass the lables toflextable::compose()
, where the labels were something likeas_paragraph(list_values = list("H", as_sub("2"), "O")
. The code is obviously too lengthy; plus i have to manipulate cells one-by-one. Technically still doable, but I do have tables with 100+ cells needed formatting.- Output first, format in Word later: Again, needed manipulation one-by-one. Would be an option if it would work everything out automatically.
- Convincing the school bureaucrats to accept html/pdf/latex as output: this is forever an impossible option.
- Format outside word then export formatted table as image: strictly forbidden in the reports.
Edit: the table works now! Great thanks to Maël's answer, but please check my own findings to see my final result: