如何在xtable中使用scalebox为调整大小的表添加标题?

时间:2017-12-04 22:11:28

标签: r r-markdown xtable

我正在研究R Markdown文档。我有一个这样的数据框架:

library(tidyverse)
library(xtable)

df <- tibble(a = 1:10, b = 1:10, c = 1:10, d = 1:10, e = 1:10, f =1:10,
g = 1:10, h = 1:10, i = 1:10)

我正在使用xtable包来创建显示表。由于表格太宽,我使用scalebox参数

缩放表格
xt <- xtable(df, caption = "Table 1")
print(xt, type = "latex", comment = FALSE,floating = F, 
include.rownames = F, scalebox = 0.50)

但是,文档上不显示标题。我该怎么办?

1 个答案:

答案 0 :(得分:1)

如果您不仅限于使用xtable,我建议您切换到knitr::kablekableExtra

---
output: pdf_document
---

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

df <- tibble(a = 1:10, b = 1:10, c = 1:10, d = 1:10, e = 1:10, f =1:10,
g = 1:10, h = 1:10, i = 1:10)
```

```{r table, results='asis'}
df %>%
  kable("latex", caption = "Table 1", booktabs = TRUE) %>%
  kable_styling(latex_options = c("striped", "hold_position"))
```

...可生产

enter image description here

此外,scale_down还有一个等效的latex_options选项。但是,正如vignette中所述,它将适合页面宽度,因此如果表格不够宽也会向上扩展。

enter image description here