Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

add_header_above() allow for supplying line argument as vector #700

Closed
tbbarr opened this issue Mar 28, 2022 · 10 comments · Fixed by #874
Closed

add_header_above() allow for supplying line argument as vector #700

tbbarr opened this issue Mar 28, 2022 · 10 comments · Fixed by #874

Comments

@tbbarr
Copy link

tbbarr commented Mar 28, 2022

Currently when adding headers to a kable object via add_header_above() if wanting to italicise/align certain headers differently this seems to be possible via a vector of arguments, e.g.:

some_kable %>%
    add_header_above(c("a header" = 1, "other header" = 2), italic=c(TRUE,FALSE), align=c("l", "c"))

However, doing the same with the line argument returns:

Warning message:
In if (line) { :
  the condition has length > 1 and only the first element will be used

It would be really nice to be able to also only draw a line under certain headers and not either all or no headers.

@krivit
Copy link

krivit commented Mar 6, 2024

Seconding this: at the moment, the code uses a heuristic that if the column in the header above is blank, then there is no line under it, and if it has any non-whitespace, then there is.

Sometimes, finer control is required. For example, as far as I can tell, there is currently no way to typeset the following header:

|----------|---------------------|
| Multirow | Column group        |
|          | ------------------- |
| name     | Col 1      Col 2    |
|----------|---------------------|
|          |                     |
|          |                     |

@tleutritz-ukw
Copy link

hi, obviously an old thread, but to my recent experience, a logical vector is accepted, when having a HTML output, but for LaTeX this won't work - any idea, why?

@dmurdoch
Copy link
Collaborator

dmurdoch commented Dec 4, 2024

@tleutritz-ukw : you should post a minimal example that shows this, and if you are especially ambitious, a PR that fixes it.

@tleutritz-ukw
Copy link

---
output:
  html_document
---
```{r setup and data import, include=FALSE}
require(knitr)
require(dplyr)
require(kableExtra)

Noten.tab = data.frame(Note = 1:5, Prz = c(90, 80, 70, 60, 0), Pkt = c(44.1, 39.2, 34.3, 29.4, 0),
                       Anzahl = c(3, 6, 3, 4, 6), Anteil = c(14, 27, 14, 18, 27),
                       p = c("▐███████", "▐██████████████", "▐███████", "▐█████████", "▐██████████████"))
Noten_col = c("#1A9641", "#A6D96A", "yellow", "#FDAE61", "#D7191C")
```

```{r table with bargraph, results="asis", echo=F}
Noten.tab = mutate(Noten.tab, Anteil = signif(Anteil, 2))
Noten_scl_num = paste0("0 %", paste0(rep(" ", 7), collapse = ""), "25 %", paste0(rep(" ", 8), collapse = ""), "50 %", paste0(rep(" ", 9), collapse = ""), "75 %", paste0(rep(" ", 7), collapse = ""), "100 %")
Noten_scl_ax = "├────────────┼───────────┼────────────┼───────────┤"
Noten_scl = paste(Noten_scl_num, Noten_scl_ax, sep = "\n")
# Noten.tab$p = cell_spec(Noten.tab$p, monospace = T, font_size = 6)
kable(Noten.tab, col.names = c("Note", "Prozent", "Punkte", "Anzahl", "Anteil", 
                               text_spec(Noten_scl_ax, format = "html", monospace = T)),
           escape = F, format = 'html') %>% 
  kable_styling(position = "left", "striped", full_width = F) %>% 
  add_header_above(c("", "Notengrenze" = 2, "Prüflinge" = 2, Noten_scl_num),
                   monospace = c(rep(F, 3), T), line = c(F, T, T, F), escape = F) %>%
  column_spec(column = 6, monospace = T, color = Noten_col)
```

@dmurdoch
Copy link
Collaborator

dmurdoch commented Dec 5, 2024

There's a lot of HTML specific code in that example, not just the line vector, so I get this error when I switch the output to pdf_document:

Error: Functions that produce HTML output found in document targeting latex output.
Please change the output type of this document to HTML.

@tleutritz-ukw
Copy link

tleutritz-ukw commented Dec 5, 2024

of course... simply replace "html" with "latex" in both format arguments...
I just wanted to demonstrate, that for html the vector for the line argument is working

@dmurdoch
Copy link
Collaborator

dmurdoch commented Dec 5, 2024

The point is that the document doesn't work in LaTeX even with those changes and setting line = FALSE. Please post a minimal example that demonstrates that a scalar line value works but a vector one doesn't.

@tleutritz-ukw
Copy link

here you go (change line argument to scalar in order to get it working):

---
output:
  pdf_document
---

```{r setup and data import, include=FALSE}
require(knitr)
require(dplyr)
require(kableExtra)

Noten.tab = data.frame(Note = 1:5, Prz = c(90, 80, 70, 60, 0), Pkt = c(44.1, 39.2, 34.3, 29.4, 0),
                       Anzahl = c(3, 6, 3, 4, 6), Anteil = c(14, 27, 14, 18, 27),
                       p = "")
Noten_col = c("#1A9641", "#A6D96A", "yellow", "#FDAE61", "#D7191C")
```

```{r table with bargraph, results="asis", echo=F}
Noten.tab = mutate(Noten.tab, Anteil = signif(Anteil, 2))
Noten_scl_num = "some long text"
Noten_scl_ax = "more long text"
kable(Noten.tab, col.names = c("Note", "Prozent", "Punkte", "Anzahl", "Anteil", 
                               text_spec(Noten_scl_ax, format = "latex", monospace = T)),
           escape = F, format = 'latex', booktabs = T) %>% 
  kable_styling(position = "left", latex_options = "striped", full_width = F) %>% 
  add_header_above(c("", "Notengrenze" = 2, "Prüflinge" = 2, Noten_scl_num),
                   monospace = c(rep(F, 3), T), line = c(F, T, T, F), escape = F) %>%
  column_spec(column = 6, monospace = T, color = Noten_col)
```

@dmurdoch
Copy link
Collaborator

dmurdoch commented Dec 5, 2024

Thanks. This should now be fixed in the Github version. Please try it out and let us know if there are still problems.

@tleutritz-ukw
Copy link

Taks a lot - it works! :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants