0

Problem description:

I'm writing a report in rMarkdown and I need to have two tables set side-by-side in an A4 PDF document.

An answer to a previous question suggested either passing a list of the tables to kable; or to insert the tables into the following latex code:

 cat(c("\\begin{table}[!htb]
    \\begin{minipage}{.5\\linewidth}
      \\caption{}
      \\centering",
        t1,
    "\\end{minipage}%
    \\begin{minipage}{.5\\linewidth}
      \\centering
        \\caption{}",
        t2,
    "\\end{minipage} 
\\end{table}"
)) 

Adding it to my code (below) produces a page of latex code in the resulting PDF, with neither of the tables I'm after. Passing the tables as a list doesn't seem to work, possibly because of the unique kableExtra formatting on each table.

Reproducible example:

---
output: pdf_document
header-includes:
- \usepackage{booktabs}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(knitr)
library(kableExtra)
library(tibble)
```
```{r echo = FALSE}
df1 <- tribble(~Location, ~Jan_Qrt, ~Jan_year, ~growth,
"New South Wales",  16000,  403300,       3.30,
"Victoria",        -21200,    134100,       3.50,
"Queensland",        2100,  100200,       3.20,
"South Australia",  19700,  117900,       5.00,
"Western Australia",   5300,   13200,     1.60,
"Tasmania",         -8900,   24300,     1.90,
"Northern Territory",    -400,    5700,    2.40,
"Australian Cap",     300,   -4300,    -3.10,
"Australia",         800,   10600,     4.80)

df2 <- read.table(text = '"Location" "qtr"  "year" "growth"
 
 "New South Wales"               " 16,000"  403,300       3.30
 "Victoria"                      -21,200    134,100       3.50
 "Queensland"                    "  2,100"  100,200       3.20
 "South Australia"               " 19,700"  117,900       5.00
 "Western Australia"             "  5,300"  " 13,200"     1.60
 "Tasmania"                      " -8,900"  " 24,300"     1.90
 "Northern Territory*"           "   -400"  "  5,700"     2.40
 "Australian Capital Territory*" "    300"  " -4,300"    -3.10
 "Australia**"                   "    800"  " 10,600"     4.80', header = T)

t1 <- kable(df1, format = 'latex', booktabs = T, align = 'r', escape = F) %>%
  kable_styling(full_width = F, font_size = 8) %>%
  row_spec(row = 9, bold = TRUE) %>%
  add_header_above(c(" ", first = 1, "TTY (persons)" = 1, "TTY (% chg.)" = 1))

t2 <- kable(df2, format = 'latex', booktabs = T, align = 'r', escape = F) %>% 
  kable_styling(full_width = F, font_size = 8)

# kable(list(t1, t2)) <- not run

cat(c("\\begin{table}[!htb]
    \\begin{minipage}{.5\\linewidth}
      \\caption{}
      \\centering",
        t1,
    "\\end{minipage}%
    \\begin{minipage}{.5\\linewidth}
      \\centering
        \\caption{}",
        t2,
    "\\end{minipage}
\\end{table}"
))
  
```
Community
  • 1
  • 1
Dom
  • 1,043
  • 2
  • 10
  • 20

0 Answers0