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.

kr32
  • 63
  • 1
  • 4

5 Answers5

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.

3

Simply replace width by width_min!

Shahab Einabadi
  • 307
  • 4
  • 15
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

Rob Smith
  • 541
  • 1
  • 4
  • 6
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.

Thomas Philips
  • 935
  • 2
  • 11
  • 22
0

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}
RobS
  • 3,807
  • 27
  • 34