Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Enable row_spec() to modify specific rows of multirow headers or provide a separate function to do so. #827

Open
krivit opened this issue Mar 6, 2024 · 3 comments

Comments

@krivit
Copy link

krivit commented Mar 6, 2024

Describe the bug

As of this writing, kable %>% row_spec(row = 0, ...) modifies the header of the table, but if the header has multiple rows (via add_header_above()), the changes are applied to the last (bottommost) header row, and there appears to be no way to address the others.

Prior to 989d6dc, it was (serendipitously, I believe) possible to affect the last-but-one row with row = -1, but this no longer works. This could serve as a convention for addressing the rows (i.e., 0 for the bottommost header, -1 for the one above it, -2 for the one above that one, etc.), though it could clash with R's indexing.

To Reproduce

In the following, only the second header row is bolded:

---
output: pdf_document
---

```{r}
library(kableExtra)
data(mtcars)
mtcars <- mtcars[1:3, 1:3]
```
```{r}
kable(mtcars) |>
  add_header_above(c("abc"=2, "def"=2)) |>
  row_spec(0, bold=TRUE)  
```
@dmurdoch
Copy link
Collaborator

dmurdoch commented Mar 6, 2024

I think a reasonable approach here would be to add another argument, something like header = FALSE, which if set to TRUE would mean the row indexing applied to the header instead of the body of the table.

I don't know what should happen with row = 0. If header = TRUE that should probably give an error. If header = FALSE, should some sort of back-compatibility be kept, or should that also be an error?

@krivit
Copy link
Author

krivit commented Mar 7, 2024

Since 0 is rarely used in R indexing, there is a strong argument for keeping it around as a special case for backwards compatibility. (Indeed, the fix that removed -1 broke some of my manuscripts.)

That having been said row_spec() never followed R's indexing semantics in the first place. For example, it does not accept logical vectors, and even after 989d6dc, a row = -1 with extra_latex_after= attempts to insert the code before the table start or thereabout, not after each table row but the first.

@krivit
Copy link
Author

krivit commented Mar 12, 2024

If header = FALSE, should some sort of back-compatibility be kept, or should that also be an error?

It looks like row = 0 modifying the header row is documented in the PDF vignette (https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_pdf.pdf, p. 15).

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants