kableExtra column_spec width not working
Asked Answered
S

5

6

I am creating tables that will be rendered using Rmarkdown to HTML. I am using kable and have been experimenting with kableExtra to add features to my tables. I am not able to get the width option in column_spec to work when applying it to all columns in a table:

data.frame(RRmin=1, RRmax=10) %>%
   dplyr::rename(`Reportable Range Min` = RRmin, `Reportable Range Max` = RRmax) %>%
   kable() %>%
   column_spec(1:2, width = "0.5in") %>%
   kable_styling(c("bordered", "condensed"), full_width = F)

This gives a table that looks like this. I can make the width longer and both columns change, but when it goes smaller it does not seem to work. I can make one column smaller but not the other:

data.frame(RRmin=1, RRmax=10) %>%
   dplyr::rename(`Reportable Range Min` = RRmin, `Reportable Range Max` = RRmax) %>%
   kable() %>%
   column_spec(1, width = "0.5in") %>%
   kable_styling(c("bordered", "condensed"), full_width = F)

This gives a table that looks like this. The first column was appropriately changed but I cannot get this effect when I'm trying to change the size of both columns. I have tried doing separate column_spec lines for each column, using escape=F and am not sure what to try next.

Scanlan answered 31/7, 2018 at 12:49 Comment(0)
C
4

I have had similar problems with column_spec not working. I was able to find a fix that worked for my purposes by playing with the width_min option. Maybe that will help.

My issue was that none of the columns widths seemed to be adjusted by column_spec, even when I tried all of the options you mention above. The result was that some columns were way too thin. I set width_min="3in" and fixed it. This was not a perfect fix because now I'm left with other column that are too wide, but it at least made my table a little more readable.

Cistercian answered 7/2, 2019 at 17:55 Comment(0)
A
3

Simply replace width by width_min!

Arbitration answered 18/12, 2019 at 17:24 Comment(0)
K
2

This may be a little late, but I've just been working with the kableExtra package, and it seems that your code is now working pretty much as is.

At first I thought it might have something to do with the ordering of the kable_styling component, but it seems not to matter which order it is in. Perhaps it was a bug in the package that has since been fixed. It is also immaterial wether you use column_spec(column = 1:2, width = "2in"), or column_spec(1:2, width = "2in"). Both seem to work well, as do modifications to the columns size. See below:

---
output: pdf_document
---

```{r global_options, include=FALSE}
# Just some setup:

sapply(c("knitr", "tidyverse", "kableExtra"), require, character.only = TRUE)

options(knitr.kable.NA = '', knitr.table.format = "latex")

knitr::opts_chunk$set(fig.path = 'figures/',
                  echo = FALSE, warning = FALSE, message = FALSE)

opts_chunk$set(echo = FALSE,
           message = FALSE,
           warning = FALSE,
           fig.align = "center",
           fig.width = 5,
           fig.pos = 'H',
           as.is = TRUE)
```

```{r variable-names-table, as.is=TRUE}
# Size example 1; 1.5 inch columns
data.frame(RRmin=1, RRmax=10) %>%
    dplyr::rename(`Reportable Range Min` = RRmin, `Reportable Range Max` = RRmax) %>%
   kable() %>%
   kable_styling(c("bordered", "condensed"), full_width = F) %>%
   column_spec(column = 1:2, width = "1.5in")

# Size example 2; 3 inches
data.frame(RRmin=1, RRmax=10) %>%
    dplyr::rename(`Reportable Range Min` = RRmin, `Reportable Range Max` = RRmax) %>%
   kable() %>%
   column_spec(column = 1:2, width = "3in") %>%
   kable_styling(c("bordered", "condensed"), full_width = F)

# To set columns 1 and two to different sizes
data.frame(RRmin=1, RRmax=10) %>%
    dplyr::rename(`Reportable Range Min` = RRmin, `Reportable Range Max` = RRmax) %>%
   kable() %>%
   column_spec(column = 1, width = "3in") %>%
   column_spec(column = 2, width = "2in") %>%
   kable_styling(c("bordered", "condensed"), full_width = F)

```

Just a note for anyone else dealing with the issue. The above will run as an RMD

R version 3.6.1, on mac RStudio 1.2.1335 kableExtra 1.1.0 knitr 1.25 tidyverse 1.2.1

Kwakiutl answered 19/11, 2019 at 3:28 Comment(3)
I still have the same problem, The bug is not fixedArbitration
Hi Shahab, if you copy and paste the code I wrote into a fresh RMD does it produce a PDF with tables of the correct widths?Kwakiutl
One additional point to mention is that this code is compiled with the Tex Live 2019 distribution.Kwakiutl
C
1

The fix is to include the array package in the latex preamble. One way of doing this is adding the following lines to your Rmarkdown header:

header-includes:
  - \usepackage{array}
Coagulum answered 1/3, 2021 at 16:0 Comment(0)
W
0

I'm using Tex Live 2020, and this problem still exists - it appears that column_spec has a bug. All thse examples run without a probllem if I remove the column_spec commands. As soon as I include the column_spec commands, I get a cryptic error which says 'Undefined control sequence, Latex Error: Illegal character in array arg. The description is also cryptic: ...n}|>{\raggedleft\arraybackslash}p{1.5in}}

The control sequence at the end of the top line of your error message was never \def'ed. If you have misspelled it (e.g., \hobx), type I and the correct spelling (e.g., I\hbox). Otherwise just continue, and I'll forget about whatever was undefined. Removing the column_spec command fixes the problem.

Whoever answered 11/1, 2021 at 20:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.