如何保存由 tableby function in landscape r 创建的 table

how to save table created by tableby function in landscape r

我正在使用 R 中 arsenal 包中的函数 tableby。为了生成漂亮的 tables,我在 RMarkdown 中使用了这个函数。然后,通过乳胶创建一个pdf文件。具体来说,我在 RMarkdown 中使用的代码是

```{r}
controls = tableby.control(test = FALSE,
                           total = TRUE,
                           totel.pos = 'before',
                           digits = 0,
                           digits.count = 0,
                           digits.pct = 1,
                           digits.n = 0,
                           numeric.stats = c('min', 'mean', 'median', 'q1q3', 'max', 'sum'), 
                           stats.labels = list(N = 'Count', min = 'Min.', mean = 'Mean', 
                                               median = 'Median', q1q3='Q1,Q3', max = 'Max.', sum = 'Sum'),
                           selectall.stats = c('N', 'Nmiss', 'countrowpct'))
```

```{r, results='asis'}
t1 = arsenal::tableby(cluster ~ ABCD.InvSales + X2020, data = data_complete_test, control = controls)
summary(t1, test = FALSE)
```

一些可重现的数据可以在这里找到

data_complete = structure(list(ABCD.InvSales = structure(c(3L, NA, NA, 2L, 3L, 
3L, 2L, 3L, 3L, 3L, 2L, 3L, 1L, 3L, 3L, NA, 3L, NA, NA, NA, 3L, 
NA, 3L, 3L, NA, 3L, NA, 2L, NA, 3L, 3L, 3L, 2L, 1L, 3L, 3L, NA, 
2L, 1L, NA, 3L, NA, 3L, NA, 3L, 1L, 3L, NA, 3L, 1L, 2L, NA, 3L, 
NA, NA, 3L, 3L, 3L, NA, 3L, 3L, NA, 3L, 3L, 3L, 3L, 3L, 2L, NA, 
NA, 1L, 4L, NA, 3L, 3L, NA, 3L, 2L, 3L, 2L, 3L, NA, 3L, 3L, 2L, 
3L, 3L, NA, 3L, 3L, 3L, NA, 3L, NA, 3L, 3L, 3L, NA, NA, NA), .Label = c("A", 
"B", "C", "D"), class = "factor"), X2020 = c(NA, NA, NA, 1904.52, 
NA, 136.04, 6457.26, NA, 595.08, 743.5, 643.35, NA, 64946.03, 
NA, 38.8, NA, 4118.7, NA, NA, NA, NA, NA, NA, NA, NA, 2674.488028, 
NA, 628.62, NA, 248.7697993, NA, NA, NA, 66165.44, 1749.68, NA, 
NA, 5110.64, 18319, NA, NA, NA, 1167.36, NA, 391.302, 16633.18, 
NA, NA, 1126.06, 6528.09, NA, NA, NA, NA, NA, 310.06, 242.24, 
NA, NA, 57.42, 122.36, NA, 486.1830396, 636, NA, 545.1747764, 
111.75, 2984.59, NA, NA, 8144.78, 31.6, NA, 52.8, 193.31, NA, 
NA, 6786.800793, NA, 4792.34, 82.12, NA, NA, NA, 2068, 114.93, 
NA, NA, NA, 192.598074, 114.34714, NA, 2846.15, NA, NA, NA, NA, 
NA, NA, NA), cluster = structure(c(1L, 2L, 1L, 3L, 2L, 4L, 3L, 
1L, 2L, 1L, 5L, 1L, 3L, 1L, 1L, 4L, 1L, 1L, 1L, 4L, 1L, 5L, 4L, 
1L, 1L, 1L, 1L, 1L, 4L, 1L, 4L, 1L, 4L, 4L, 3L, 1L, 2L, 4L, 2L, 
1L, 1L, 3L, 1L, 1L, 4L, 2L, 2L, 4L, 1L, 2L, 4L, 1L, 4L, 1L, 4L, 
4L, 1L, 2L, 1L, 1L, 3L, 1L, 2L, 4L, 4L, 1L, 4L, 5L, 4L, 5L, 5L, 
2L, 1L, 1L, 1L, 3L, 1L, 4L, 2L, 4L, 2L, 2L, 1L, 4L, 4L, 1L, 1L, 
1L, 1L, 4L, 4L, 1L, 1L, 3L, 4L, 1L, 1L, 3L, 1L, 1L), .Label = c("1", 
"2", "3", "4", "5"), class = "factor")), row.names = c(NA, 100L
), class = "data.frame")

我很满意 table 生成的 pdf 文件。但是,我的问题是如何能够以横向格式保存生成的 pdf table 以便在一页上容纳更多列?

如果有什么不清楚的地方,请告诉我,我会尽力解释!

好的,经过一些研究,我发现以下两个解决方案对我来说都很好用。 This 这个问题对我帮助很大,我现在把两个答案结合起来。

当您想将PDF文件编织成横向模式时,您唯一需要添加的是classoption: landscape。也就是说,

title: "Landscape and Portrait"
author: "Jung-Han Wang"
date: "Thursday, March 19, 2015"
output: pdf_document
classoption: landscape

然后,如果您想要一些纵向和横向模式的页面,可以避免创建单独的 header.tex 文件的可能性是在 YAML header 中按以下方式定义它

---
title: "Mixing portrait and landscape WITHOUT a header.tex file"
header-includes:
- \usepackage{pdflscape}
- \newcommand{\blandscape}{\begin{landscape}}
- \newcommand{\elandscape}{\end{landscape}}
output: pdf_document
---

Portrait
```{r}
summary(cars)
```

\newpage
\blandscape
Landscape
```{r}
summary(cars)
```
\elandscape

\newpage
More portrait
```{r}
summary(cars)
```