2

I am using the tabular() function in the tables package to make tables in an R Markdown file. I would like to use the booktabs() option to include a horizontal rule. However, when I do so, the code from booktabs() appears in my LaTex document, even when I set the chuck option to echo=FALSE.

How can I use the booktabs() option with tabular?

Output: tables with booktabs output

And here is the code for the example:

---
title: "Making Tables"
output:
  pdf_document: default
header-includes: \usepackage{booktabs}
---

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

library(tables)
library(Hmisc)
```

```{r no line, results='asis'}
latex(tabular( (Species + 1) ~ (n=1) + Format(digits=2)*(Sepal.Length + Sepal.Width)*(mean + sd), data = iris))
```

```{r with line, results='asis', echo = FALSE}
booktabs()
latex(tabular( (Species + 1) ~ (n=1) + Format(digits=2)*(Sepal.Length + Sepal.Width)*(mean + sd), data = iris))
```
missng
  • 199
  • 13
  • Would you be open to using kableExtra instead, or is there a technical reason you have chosen this route? – Michael Harper Nov 16 '17 at 01:11
  • 1
    I liked the aesthetics of the tables package better, but if it's easy to do in kableExtra I would be open to it. It also seemed easier use with cross-tabulated data. – missng Nov 16 '17 at 01:20

1 Answers1

2

Just use the invisible function around booktabs like this:

```{r with line, echo = FALSE,results= "asis"}
invisible(booktabs())
latex(tabular( (Species + 1) ~ (n=1) + Format(digits=2)*(Sepal.Length + Sepal.Width)*(mean + sd), data = iris))
```
Alejandro Andrade
  • 2,196
  • 21
  • 40