【问题标题】:knitr::kable(x, col.names=NULL) doesn't works when the output is a pdf当输出为 pdf 时,knitr::kable(x, col.names=NULL) 不起作用
【发布时间】:2021-12-13 17:38:46
【问题描述】:

我需要删除使用 kable 生成的表中的变量名称,并将其导出为 pdf

kable(x, col.names = NULL) 输出为 html 格式时有效,但输出为 pdf 时无法编译并抛出错误:“add_intent_latex...”

【问题讨论】:

  • 认为我之前确实使用了一种解决方法,其中我用空格重命名了列,例如df = df %>% rename(` ` = Var1, ` ` = Var2) 等 - Var1 一个空格,Var2 等两个空格等,以保持 colnames 的唯一性。这行得通吗?
  • 谢谢。这是我正在采用的临时解决方案。但这不是最优的,因为有一个空格

标签: r pdf latex knitr kable


【解决方案1】:

试试这个解决方案,我为你准备了一个模板:

---
title: "Hello World"

output:
  pdf_document: 
       latex_engine: xelatex
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r, include = FALSE}
library(kableExtra)

my_data <-  mtcars[1:5, 1:6]
my_table <- my_data %>%
        kbl(caption = "without col.names", col.names = NULL) %>%
           kable_styling(bootstrap_options = c("striped", "hover"), latex_options = "hold_position")   


my_table1 <- my_data %>%
        kbl(caption = "with col.names") %>%
           kable_styling(bootstrap_options = c("striped", "hover"), latex_options = "hold_position")
```


```{r}
my_table
```

```{r}
my_table1
```

【讨论】:

    猜你喜欢
    • 2019-04-08
    • 1970-01-01
    • 2020-10-02
    • 2020-08-01
    • 1970-01-01
    • 2018-04-20
    • 2019-08-16
    • 2015-11-22
    • 1970-01-01
    相关资源
    最近更新 更多