Search code examples
rpdfsavelatexlandscape

how to save table created by tableby function in landscape r


I am using the function tableby from the arsenal package in R. To produce beautiful tables I am using this function in RMarkdown. Then, a pdf file is created via latex. Specifically, the code I am using in RMarkdown is

```{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)
```

and some reproducible data can be found here

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")

I am satisfied with the table which is produced in a pdf file. But, my question is how to be able to save the resulting pdf table in landscape format in order to fit more columns on one page?

If anything is unclear, please let me know and I will do my best to explain!


Solution

  • Okay, after some research I found the following two solutions which both worked fine for me. This question helped me a lot and I just combine two answers now.

    When you want to knit the PDF file to landscape mode, the only thing you need to add is classoption: landscape. That is,

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

    Then, if you want some pages in portrait and in landscape mode a possibility to avoid the necessity to create a separate header.tex file is to define it in the YAML header the following way

    ---
    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)
    ```