dorota
dorota

Reputation: 43

column_spec function in kableExtra in R doesn't work

I want co change column width in pdf with kable ( , 'latex') but the fucntion doesn't work. Anybody know why? here is my code:

   table = knitr::kable(jeden, "latex" , row.names = F ,  align = "llrrrrrrrrrr" , escape = F, booktabs = F, caption = '1. Sprzedaz uslug i towarow razem') 
   kableExtra::column_spec(table, 1, width = "1cm", bold = TRUE, italic = TRUE)

Upvotes: 3

Views: 7730

Answers (2)

Hao
Hao

Reputation: 7826

It's not a bug but rather a relatively strange setting for align in knitr::kable(). In xtable you can put align in a string but for kable, you will have to provide a vector. In your case, if you put things like align = c(rep("l", 2), rep("r"), 2), you should be fine.

Upvotes: 2

Maurits Evers
Maurits Evers

Reputation: 50668

It seems that align breaks your column_spec, but only for LaTeX/PDF output.

Here are two minimal & reproducible examples.

PDF output

---
title: "Untitled"
output:
  pdf_document: default
---

```{r}
library(knitr)
library(kableExtra)
x <- kable(head(mtcars[, 1:4]), "latex", row.names = F, align = "llrr")
column_spec(x, 1:2, width = "4cm", bold = TRUE, italic = TRUE)
```

enter image description here

If you remove align from the PDF RMarkdown document, column_spec works as expected.

HTML output

---
title: "Untitled"
output:
  html_document: default
---

```{r}
library(knitr)
library(kableExtra)
x <- kable(head(mtcars[, 1:4]), "html", row.names = F, align = "llrr")
column_spec(x, 1:2, width = "4cm", bold = TRUE, italic = TRUE)
```

enter image description here

This seems like a bug to me, and I would suggest opening an issue on the kableExtra GitHub site. If you do, you should reference this post, and include a minimal & reproducible example (similar to what I did).

Upvotes: 0

Related Questions