RMarkdown table 中的科学格式、下标和上标(docx 输出)

Scientific formats, subscripts and superscripts in RMarkdown table (docx output)

假设我有以下 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)
```

产生 this table in Word.

问题

我需要科学格式来使用上标和乘号(即 2.998 × 108),有些单元格需要下标(例如 NA 和 H2O).

The final table should look like this。我该怎么做?

我有tried/would从未尝试过

  1. huxtable 包及其 markdown() function: I managed to format some contents as H~2~O, then enable markdown across table by huxtable(a) %>% `markdown<-`(TRUE). Which did not recognize the syntax, and apparently would not work in forseeable future according to the author.
  2. flextableas_sub():生成正确的格式。我将标签传递给 flextable::compose(),其中标签类似于 as_paragraph(list_values = list("H", as_sub("2"), "O")。代码显然太长了;另外,我必须 一个接一个地操作细胞 。技术上仍然可行,但我确实有 table 需要格式化 100 多个单元格。
  3. 先输出,再在Word中格式化:同样,需要一个一个地操作。如果它能自动.
  4. 解决所有问题,那将是一个选择
  5. 说服学校官员接受html/pdf/latex作为输出:这永远是不可能的选择。
  6. 格式化外部单词然后将格式化的table导出为图像:在报告中严格禁止。

编辑: table 现在可用了!非常感谢, but please check 看到我的最终结果:

您的代码应如下所示:

```
{r table, echo=F, warning=F, message=F}
library(tidyverse)
a <- tibble(
  constants = c("c", "NA", "h", "e", "$H_2O$"),
  values = c(".998 * {10^{8}}$", ".022 * {10^{-23}}$", ".626 * {10^{-34}}$", "$-1.602 * {10 ^{-19}}$", ".802 * {10^{1}}$")
)

knitr::kable(format = "html", a, digits = 35)
```

这会给你这样的输出:

您可以使用波浪号 (~) 输入下标,使用脱字符号 (^) 输入上标;并使用 sprintf 获得预期的数字格式:

---
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)

expSup <- function(x, digits=3) {
  sprintf(paste0("%05.", digits, "f x 10^%d^"), x/10^floor(log10(abs(x))), floor(log10(abs(x))))
}

a <- tibble(
  constants = c("c", "N~A~", "h", "e", "H~2~0"),
  values = expSup(c(2.998e8, 6.022e-23, 6.626e-34, -1.602e-19, 18.02))
)

knitr::kable(a)
```

一个可能落在 no-go 区域的选项 open issue:

  1. 创建一个 html 文档,
  2. 为下标插入 html 个标签,
  3. 打开 html 文件(不是查看器),
  4. ctrl+c,然后在你的word文件中ctrl+v。
---
output: html_document
---

```{r table, echo=F, warning=F, message=F}
library(tidyverse)
library(gt)
a <- tibble(
    constants = c("c", "N<sub>A</sub>", "h","e","H<sub>2</sub>O"),
    values = c(2.998e8, 6.022e23, 6.626e-34, -1.602e-19, 18.02)
)
a %>%
  mutate(constants = map(constants, html)) %>%
  gt() %>%
  fmt_scientific(values)

中的expSup()函数将科学格式转换为markdown格式。对于我的脚本,我稍微修改了函数:

exp_sup <- function(x, digits = 3) {
  sprintf(paste0("%05.", digits, "f $\times$ 10^%d^"), x / 10^floor(log10(abs(x))), floor(log10(abs(x))))
}

我把"f x 10^%d^"改成了"f $\times$ 10^%d^",这样它就可以显示正确的乘号(×)了。

使用flextable

中的格式

这种格式在 Kable 中效果很好。但是,我的大部分工作流程需要 flextable 来制作 caption/cross reference/publication style/etc。不幸的是,虽然 expSup 函数自动将科学记数法格式化为 markdowns,但它不能使 markdown 语法在 flextable.

中工作

不过,ftExtra::colformat_md()可以。因此,通过将修改后的 exp_sup() 函数与 ftExtra 相结合,我终于能够生成 academic-looking table:

下面是我最终输出的代码;如果您还想制作包含大量 table Word 格式的可重现学术报告,希望这对您有所帮助!

---
title: "The tables work!"
author: "Satisfied Student"
date: "2022/01/28"
output: 
  bookdown::word_document2:
    reference_docx: styleRef.docx
---
```{r setup, include = F}
library(easypackages)
packages(
  "tidyverse",
  "flextable", # This works best for my workflow
  "ftExtra", # For markdown formatting work in flextable
  "officer" # You can customize appearance/format/etc. of caption *prefixes*
)

knitr::opts_chunk$set(
  warning = FALSE,
  message = FALSE,
  echo = FALSE,

  # Make the table caption format definable in reference_docx styles
  tab.cap.style = "Table Caption",

  # Make "Table 1:" prefixes not bold
  tab.cap.fp_text = fp_text_lite(bold = FALSE)

  # The tab.cap settings MUST be in a separate chunk from tables
)

# Converts scientific format to markdown
exp_sup <- function(x, digits = 3) {
  sprintf(paste0("%05.", digits, "f $\times$ 10^%d^"), x / 10^floor(log10(abs(x))), floor(log10(abs(x))))
}
# The $\times$ makes proper multiply symbols
```

```{r table}
a <- tibble(
  constants = c("c", "N~A~", "h", "e", "H~2~O"),
  values = c(2.998e8, 6.022e23, 6.626e-34, -1.602e-19, 18.02)
)

a %>%
  mutate(values = exp_sup(values)) %>%
  flextable() %>%
  set_caption(
    caption = "(ref:foo)", # Produces formatted caption text
    style = "Table Caption"
  ) %>%
  colformat_md() %>% # Subscript/superscript works in flextable now!
  theme_booktabs() %>% # The 3-part-table used in academics
  align(align = "center", part = "all") %>% #Align everything to center
  set_table_properties(layout = "autofit") # Comfortable width/height every cell
```

(ref:foo) A scientifically formatted `flextable` with ^superscripts^ and ~subscripts~