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

kable: values with newlines break tables #2255

Closed
3 tasks done
aronatkins opened this issue May 2, 2023 · 6 comments
Closed
3 tasks done

kable: values with newlines break tables #2255

aronatkins opened this issue May 2, 2023 · 6 comments

Comments

@aronatkins
Copy link
Contributor

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:

image

This is because the Imports column contains embedded newlines.

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 58e7879 (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 have fully read the issue guide at https://yihui.org/issue/.
  • I have provided the necessary information about my issue.
    • If I'm asking a question, I have already asked it on Stack Overflow or RStudio Community, waited for at least 24 hours, and included a link to my question there.
    • If I'm filing a bug report, I have included a minimal, self-contained, and reproducible example, and have also included xfun::session_info('knitr'). I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version: remotes::install_github('yihui/knitr').
    • If I have posted the same issue elsewhere, I have also mentioned it in this issue.
  • I have learned the Github Markdown syntax, and formatted my issue correctly.

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

@yihui
Copy link
Owner

yihui commented May 2, 2023

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
Copy link
Contributor Author

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
Copy link
Owner

yihui commented May 2, 2023

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
Copy link
Contributor Author

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 yihui closed this as completed in 00ec36e May 23, 2023
@yihui
Copy link
Owner

yihui commented May 23, 2023

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

Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants