簡體   English   中英

RMarkdown 表中的科學格式、下標和上標(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)
```

在 Word 中生成此表

問題

我需要科學格式來使用上標和乘號(即 2.998 × 10 8 ),有些單元格需要下標(例如 N A和 H 2 O)。

決賽桌應該是這樣的 我怎樣才能做到這一點?

我嘗試過/永遠不會嘗試的

  1. huxtable package 及其markdown() function :我設法將一些內容格式化為H~2~O ,然后通過huxtable(a) %>% `markdown<-`(TRUE)啟用 markdown< 根據作者的說法,它不識別語法,顯然在可預見的將來不會起作用。
  2. flextableas_sub() :產生正確的格式。 我將標簽傳遞給flextable::compose() ,其中標簽類似於as_paragraph(list_values = list("H", as_sub("2"), "O") 。代碼顯然太長了;另外我有一個接一個地操作單元格。技術上仍然可行,但我確實有需要格式化的 100 多個單元格的表格。
  3. 首先是 Output,然后在 Word 中格式化:同樣,需要逐個操作。 如果它能自動解決所有問題,那將是一個選擇。
  4. 說服學校官僚接受html/pdf/latex 作為 output :這永遠是不可能的選擇。
  5. 格式化外部單詞,然后將格式化的表格導出為圖像:報告中嚴格禁止。

編輯:桌子現在可以工作了! 非常感謝Maël 的回答,但請檢查我自己的發現以查看我的最終結果:

格式良好的表格

您可以使用波浪號 ( ~ ) 輸入下標,插入符號 ( ^ ) 表示上標; 並使用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)
```

在此處輸入圖像描述

一個可能屬於此開放問題的禁區的選項:

  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)

Maël 的答案中的expSup() function 將科學格式轉換為 markdown 格式。 對於我的腳本,我稍微修改了 function:

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

我將"fx 10^%d^"更改為"f $\\times$ 10^%d^" ,以便它顯示正確的乘法符號 (×)。

使用flextable中的格式

該格式在Kable中效果很好。 但是,我的大部分工作流程都需要flextable來制作標題/交叉引用/發布樣式/等。 不幸的是,雖然expSup function 自動將科學記數法格式化為降價,但它不能使 markdown 語法在 flextable 中工作。

但是, ftExtra::colformat_md()可以。 因此,通過將修改后的exp_sup() function 與ftExtra結合起來,我終於能夠生成一個學術外觀的表格:

最終格式化結果

下面是我最終的 output 的代碼; 如果您還嘗試使用大量 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~

您的代碼應如下所示:

{r table, echo=F, warning=F, message=F}
library(tidyverse)
a <- tibble(
  constants = c("c", "NA", "h", "e", "$H_2O$"),
  values = c("$2.998 * {10^{8}}$", "$6.022 * {10^{-23}}$", "$6.626 * {10^{-34}}$", "$-1.602 * {10 ^{-19}}$", "$1.802 * {10^{1}}$")
)

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

這會給你 output 像這樣:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM