0

I'm working on a R Markdown document. I've got a dataframe of this kind:

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)

I'm using the xtable package to create display a table. Since the table is too wide, I scale the table with the scalebox parameter

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

However, the caption is not displayed on the document. What can I do?

jandraor
  • 187
  • 1
  • 10
  • 1
    Thanks. It worked by adding the parameter `table.placement = "!h"` & `floating = TRUE` – jandraor Dec 04 '17 at 23:32
  • It is generally considered bad practice to use `h` on its own (see [here](https://tex.stackexchange.com/questions/132106/difference-between-h-and-h-in-float-position)). You may also choose `table.placement = "H"` if you are trying to strictly hold the float in position. – Kevin Arseneau Dec 04 '17 at 23:40
  • Thanks. It worked as well. – jandraor Dec 05 '17 at 14:32

1 Answers1

1

If you are not limited to using xtable, I would recommend making the switch to knitr::kable and kableExtra.

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

Produces...

enter image description here

In addition, there is an equivalent scale_down option available to latex_options. However, as noted in the vignette it will fit to page width and therefore also scale up if the table is not sufficiently wide.

enter image description here

Kevin Arseneau
  • 6,186
  • 1
  • 21
  • 40