rmarkdown & kable/kableextra : Printing % symbol in Table when using escape = F

标签 r latex r-markdown kable kableextra

我想使用 rmarkdwon 和 kable 在 pdf 文档中创建一个表格。
在标题中必须有一个换行符,因此我需要设置转义 = F。
该表本身包含作为字符串的百分比值,包括 % 符号(例如:“13%”)。
我在这里找到 https://tex.stackexchange.com/questions/34580/escape-character-in-latex % 是 La(Tex) 中的一个特殊字符,我必须使用\% 排除它。
但是如何在 R 中创建和存储字符串“\%”?
我已经尝试了以下但没有对我有用:

gsub("%", "\%", c("14%", "15%", "16%"))
Error: '\%' is an unrecognized escape in character string starting ""\%"

gsub("%", "\\%", c("14%", "15%", "16%"))
[1] "14%" "15%" "16%"

> cat("\\%")
\%

gsub("%", cat("\\%"), c("14%", "15%", "16%"))
\%
Error in gsub("%", cat("\\%"), c("14%", "15%", "16%")) : 
  invalid 'replacement' argument

有人知道这个问题的解决方案吗?

提前致谢

编辑:
这是我的问题的一些示例代码:

编辑编辑:
在我制作的 data.frame testtable 中,我忘记设置 stringsAsFactors = F。
这就是导致我的真实数据结果存在差异的原因。
我已将其添加到代码中并更新了输出图片
    ---
    title: "Table with % and linbebreak"
    output:
      # html_document: default
      pdf_document:
        keep_tex: yes
      fig_caption: yes
    lang: de
    ---


    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = FALSE, warning = FALSE, error = FALSE, dev=c('pdf','png'), dpi=500)

    library(dplyr)
    library(data.table)
    library(reshape2)
    library(knitr)
    library(markdown)
    library(kableExtra)
    library(psych)
    library(survey)
    library(ggplot2)

    set.seed(123)
    testtable = data.frame(matrix(paste0(sample(1:100,16, replace = T), "%"), nrow = 4, byrow = T), stringsAsFactors = F)
    testtable$group = c("Var1", "Var2", "Var3", "Var4")
    testtable = testtable[c(5,1:4)]
    colnames(testtable) = c("Variable","All\n(n = 300)", "Group1\n(n = 120)", "Group2\n(n = 100)", "Group3\n(n = 80)")

    ```
    # Table including %-symbol, escape=F not set

    ```{r}
    testtable %>% mutate_all(linebreak) %>% 
    kable(format = "latex", align = c("l", "c", "c", "c", "c"), caption = "Caption", booktabs = T, col.names = linebreak(names(testtable), align = "c")) %>%
      kable_styling(position = "center", latex_options = c("hold_position")) %>%
      footnote(general = "* This is a note to show what * shows in this table plus some addidtional words to make this string a bit longer. Still a bit more", threeparttable = T, general_title = "Anmerkung:", title_format = "italic")
    ```

    # Table including %-symbol, escape=F
    This leads to an Error, see pic below

    ```{r}
    testtable %>% mutate_all(linebreak) %>% 
    kable(format = "latex", align = c("l", "c", "c", "c", "c"), caption = "Caption", booktabs = T, escape = F, col.names = linebreak(names(testtable), align = "c")) %>%
      kable_styling(position = "center", latex_options = c("hold_position")) %>%
      footnote(general = "* This is a note to show what * shows in this table plus some addidtional words to make this string a bit longer. Still a bit more", threeparttable = T, general_title = "Anmerkung:", title_format = "italic")
    ```

    # Table without %-symbol, escape=F set

    ```{r}
    # removing the %
    testtable[,2:5] = apply(testtable[,2:5],2,function(f) gsub("%", "", f))

    testtable %>% mutate_all(linebreak) %>% 
    kable(format = "latex", align = c("l", "c", "c", "c", "c"), caption = "Caption", booktabs = T, escape = F, col.names = linebreak(names(testtable), align = "c")) %>%
      kable_styling(position = "center", latex_options = c("hold_position")) %>%
      footnote(general = "* This is a note to show what * shows in this table plus some addidtional words to make this string a bit longer. Still a bit more", threeparttable = T, general_title = "Anmerkung:", title_format = "italic")
    ```

不知何故 %-symbol 没有显示。
我不知道它是怎么来的,因为我得到了其他相同样式的表(只是没有换行符)并且 %-symbol 很好用。

enter image description here

enter image description here

enter image description here

最佳答案

使用方式 2 与 gsub("%", "\\\\%", f)) .

---
title: "Untitled"
author: "Stéphane Laurent"
date: "28 août 2018"
output: 
  pdf_document: 
    keep_tex: yes
editor_options: 
  chunk_output_type: console
---


```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = FALSE, warning = FALSE, error = FALSE, dev=c('pdf','png'), dpi=500)

    library(dplyr)
    library(data.table)
    library(reshape2)
    library(knitr)
    library(markdown)
    library(kableExtra)
    library(psych)
    library(survey)
    library(ggplot2)

    set.seed(123)
    testtable = data.frame(matrix(paste0(sample(1:100,16, replace = T), "%"), nrow = 4, byrow = T), stringsAsFactors = F)
    testtable$group = c("Var1", "Var2", "Var3", "Var4")
    testtable = testtable[c(5,1:4)]
    colnames(testtable) = c("Variable","All\n(n = 300)", "Group1\n(n = 120)", "Group2\n(n = 100)", "Group3\n(n = 80)")
```

# Table including %-symbol, escape=F not set

```{r}
    testtable %>% mutate_all(linebreak) %>% 
    kable(format = "latex", align = c("l", "c", "c", "c", "c"), caption = "Caption", booktabs = T, col.names = linebreak(names(testtable), align = "c")) %>%
      kable_styling(position = "center", latex_options = c("hold_position")) %>%
      footnote(general = "* This is a note to show what * shows in this table plus some addidtional words to make this string a bit longer. Still a bit more", threeparttable = T, general_title = "Anmerkung:", title_format = "italic")
```


# Table including %-symbol, escape=F

    This leads to an Error, see pic below

```{r}
testtable[,2:5] = apply(testtable[,2:5],2,function(f) gsub("%", "\\\\%", f))
    testtable %>% mutate_all(linebreak) %>% 
    kable(format = "latex", align = c("l", "c", "c", "c", "c"), caption = "Caption", booktabs = T, escape = F, col.names = linebreak(names(testtable), align = "c")) %>%
      kable_styling(position = "center", latex_options = c("hold_position")) %>%
      footnote(general = "* This is a note to show what * shows in this table plus some addidtional words to make this string a bit longer. Still a bit more", threeparttable = T, general_title = "Anmerkung:", title_format = "italic")
```

enter image description here

这不正是你想要的吗?否则我不明白。

关于rmarkdown & kable/kableextra : Printing % symbol in Table when using escape = F,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52037040/

相关文章:

r - 在 R 中的 mutate 语句中调用 API

r - 如何在 RMarkdown 文档中添加空格?

r - 从 Rmd 文档一次执行所有 R block

reference - Latex:在同一位置引用两个同名作者的引用文献时如何避免省略作者姓名

latex - 使用 esttab 创建并排汇总统计表

latex - 如何使表格中的多行文本垂直居中

r - 如何在命令行中复制 Knit HTML?

r - IBrokers需要历史 future 合约数据吗?

r - 将日期转换为该周的星期一

r - 如何减小 knitr 生成的 pdf 文件的大小?