括号中的整数在 RMarkdown 脚本中被强制转换为 kable table 中的异常格式
Whole number in brackets gets coerced into unusual format in kable table in RMarkdown script
我有一个基本的 dataframe/tibble 显示为:
Variable `Attended Patients` `Programme Average (Weighted Mean/%)`
<chr> <chr> <chr>
1 Less than 'O' level 17149 (56.7) (56.9)
2 'O' level 9419 (31.2) (31)
3 'A' level 2821 (9.3) (9.3)
4 University/college 680 (2.2) (2.3)
5 University degree 132 (0.4) (0.5)
6 Postgraduate/professional 26 (0.1) (0.1)
我想在 RMarkdown 脚本中可视化。通过内置的 RStudio 查看器,table 看起来不错:
foo %>% knitr::kable() %>%
kableExtra::kable_styling(c("striped", "condensed"), full_width = T)
然而,当我想在 RMarkdown 脚本中创建 table 时,"O-level Variable" 的程序平均值已删除括号并在末尾添加小数,我可以'不知道为什么。
示例 RMarkdown 脚本和 table:
---
title: "Untitled"
output: html_document
---
```{r test, echo = F}
library(knitr)
library(tidyverse)
library(kableExtra)
foo <- structure(
list(
Variable = c(
"Less than 'O' level", "'O' level", "'A' level", "University/college", "University degree", "Postgraduate/professional"),
`Attended Patients` = c("17149 (56.7)", "9419 (31.2)", "2821 (9.3)", "680 (2.2)", "132 (0.4)", "26 (0.1)"),
`Programme Average (Weighted Mean/%)` = c("(56.9)", "(31)", "(9.3)", "(2.3)", "(0.5)", "(0.1)")
),
row.names = c(NA, -6L),
class = c("tbl_df", "tbl", "data.frame")
)
foo %>% kable() %>%
kable_styling(c("striped", "condensed"), full_width = T)
```
问题是 (31)
是 Pandoc Markdown 用来指示有序列表的方式之一。要禁用此功能,请将这些行放入 YAML header:
output:
html_document:
md_extensions: "-fancy_lists"
这意味着您需要对文档中的所有有序列表使用简单格式,即数字后跟句点。
编辑添加:
如果您想在文档的其他地方使用花哨的列表语法,您可以使用反斜杠(在 R 字符串中必须是双反斜杠)转义括号,例如"(31)"
会变成 "\(31)"
.
我有一个基本的 dataframe/tibble 显示为:
Variable `Attended Patients` `Programme Average (Weighted Mean/%)`
<chr> <chr> <chr>
1 Less than 'O' level 17149 (56.7) (56.9)
2 'O' level 9419 (31.2) (31)
3 'A' level 2821 (9.3) (9.3)
4 University/college 680 (2.2) (2.3)
5 University degree 132 (0.4) (0.5)
6 Postgraduate/professional 26 (0.1) (0.1)
我想在 RMarkdown 脚本中可视化。通过内置的 RStudio 查看器,table 看起来不错:
foo %>% knitr::kable() %>%
kableExtra::kable_styling(c("striped", "condensed"), full_width = T)
然而,当我想在 RMarkdown 脚本中创建 table 时,"O-level Variable" 的程序平均值已删除括号并在末尾添加小数,我可以'不知道为什么。
示例 RMarkdown 脚本和 table:
---
title: "Untitled"
output: html_document
---
```{r test, echo = F}
library(knitr)
library(tidyverse)
library(kableExtra)
foo <- structure(
list(
Variable = c(
"Less than 'O' level", "'O' level", "'A' level", "University/college", "University degree", "Postgraduate/professional"),
`Attended Patients` = c("17149 (56.7)", "9419 (31.2)", "2821 (9.3)", "680 (2.2)", "132 (0.4)", "26 (0.1)"),
`Programme Average (Weighted Mean/%)` = c("(56.9)", "(31)", "(9.3)", "(2.3)", "(0.5)", "(0.1)")
),
row.names = c(NA, -6L),
class = c("tbl_df", "tbl", "data.frame")
)
foo %>% kable() %>%
kable_styling(c("striped", "condensed"), full_width = T)
```
问题是 (31)
是 Pandoc Markdown 用来指示有序列表的方式之一。要禁用此功能,请将这些行放入 YAML header:
output:
html_document:
md_extensions: "-fancy_lists"
这意味着您需要对文档中的所有有序列表使用简单格式,即数字后跟句点。
编辑添加:
如果您想在文档的其他地方使用花哨的列表语法,您可以使用反斜杠(在 R 字符串中必须是双反斜杠)转义括号,例如"(31)"
会变成 "\(31)"
.