yihui / knitr

A general-purpose tool for dynamic report generation in R
https://yihui.org/knitr/
2.36k stars 873 forks source link

kable: values with newlines break tables #2255

Closed aronatkins closed 1 year ago

aronatkins commented 1 year ago

Given the Quarto document:

---
title: available.packages
---

```{r}
#| echo: false

library(knitr)

# set repos because R options are not inherited from the IDE.
options(repos = c(CRAN = "https://cran.rstudio.com/"))
ap <- as.data.frame(available.packages(), stringsAsFactors = FALSE)
ap <- head(ap, 50)
# Workaround: Rewrite embedded newlines.
#ap$Imports <- gsub("\\n", " ", ap$Imports)
ap <- ap[ap$Package == "abmR", ]
knitr::kable(ap)

the rendered result:

<img width="786" alt="image" src="https://user-images.githubusercontent.com/362187/235725917-89de96a8-1530-4762-8ec8-113a7f3a9886.png">

This is because the `Imports` column contains embedded newlines.

```r
ap$Imports
#> [1] "sp, table1, googledrive, swfscMisc, geosphere, kableExtra,\ngtsummary, ggplot2, gstat, purrr, sf, tmap, raster, utils,\nstats, methods, rgeos, rnaturalearth"

It appears as if escape=TRUE (the default) is not rewriting the newlines and, as a result, the table formatting is broken.

Workaround: Rewrite values to remove newlines.

Verified that this is an issue even as of 58e7879abda492fc4358dab2d249a30f581f0593 (current CRAN is knitr-1.42).

R version 3.6.3 (2020-02-29)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: OS X Snow Leopard 12.6.5, RStudio 2023.5.0.325

Locale: en_US.UTF-8 / en_US.UTF-8 / en_US.UTF-8 / C / en_US.UTF-8 / en_US.UTF-8

Package version:
  evaluate_0.20   graphics_3.6.3  grDevices_3.6.3 highr_0.10      knitr_1.42.8   
  methods_3.6.3   stats_3.6.3     tools_3.6.3     utils_3.6.3     xfun_0.39      
  yaml_2.3.7     

Using the most recent IDE daily: 2023.05.0-daily+325

quarto::quarto_version()
#> [1] ‘1.3.340’

By filing an issue to this repo, I promise that

I understand that my issue may be closed if I don't fulfill my promises.

yihui commented 1 year ago

This has been reported a few times before, e.g., #1319 #1328 #1768 #2021

Your case is relatively easy to deal with, since the newline is meaningless and can be safely removed, but a more general solution will require more work in kable() (when newlines are not meaningless). For now, I tend to just let you go with the workaround, or use other packages that do support newlines. In the long run, I wish we could fix it, but I'm not sure how to properly fix it (should we simply remove all newlines when escape = TRUE?). Thanks!

aronatkins commented 1 year ago

Agreed -- my case is straightforward; I just need to remove the newlines (because I know they are meaningless in this case).

ap$Imports <- gsub("\\n", " ", ap$Imports)
ap$Suggests <- gsub("\\n", " ", ap$Suggests)
ap$LinkingTo <- gsub("\\n", " ", ap$LinkingTo)

The more general case is more complicated (values that use multiple newlines to separate chunks of text, for example), but isn't easily expressed with Markdown tables constructed using pipe separators.

Are there cases where embedded newlines can produce valid tables?

yihui commented 1 year ago

To support newlines in Markdown tables, I think we must use other table formats. Pipe tables require each row to be a single line. Only multiline tables and grid tables support newlines: https://pandoc.org/MANUAL.html#tables These types of tables will require some substantial work in kable().

aronatkins commented 1 year ago

That's what thought, too. In other words: If kable is opinionated about how it produces tables, and always uses pipes, then converting newlines to whitespace (when escape=TRUE) causes some tables to correctly render (without workarounds like mine) and will not break existing, working tables.

There are lots of other table-building options if folks want rich content within the cells.

I'm also fine if you decide to won't-fix this; I looked at open issues but neglected to search for closed issues when filing this one.

yihui commented 1 year ago

You can use kable(..., newline = ' ') now.

github-actions[bot] commented 7 months ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.