我想使用 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 很好用。
最佳答案
将方式 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")
```
这不正是你想要的吗?否则我不明白。
关于rmarkdown 和 kable/kableextra : Printing % symbol in Table when using escape = F,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52037040/