rstudio / gt

Easily generate information-rich, publication-quality tables from R
https://gt.rstudio.com
Other
1.99k stars 200 forks source link

Make `<br>` a universal linebreak across formats #1767

Open olivroy opened 1 month ago

olivroy commented 1 month ago

I think one thing to make "just work" is recognizing <br> as a line break in all output contexts; this'll avoid any need to call fmt_markdown(). This could probably be done in the process_text() function.

We incidentally also need a similar bit of 'extra' markup to represent cross-output tab stops but that's for another issue.

Originally posted by @rich-iannone in https://github.com/rstudio/gt/issues/1765#issuecomment-2221639787


As of gt 0.11

For rtf

the text_grob argument of as_gtable() can be used.

https://github.com/rstudio/gt/pull/1776#issuecomment-2225651976

ddsjoberg commented 1 month ago

If possible, would it makes sense to make \n the universal line breaker, since that's how it's already interpreted in many other spaces? Then no one needs to learn another

teunbrand commented 1 month ago

I second the suggestion to make \n to universal line breaker. I don't understand why <br> would be the preferred option.

olivroy commented 1 month ago

I think my main reasoning is that even if gt supports all formats, it still remains an html-first format. When I construct a complex gt table, I do it interactively in html, and hope for the best when trying to convert to other formats. I am doing so blindly (too much sometimes).

So getting a LaTeX output or grid or docx is a matter of converting the html to this other format as close as possible to the html output.

Similarly, ggplot2 is grid first. Therefore, to make it interactive, plotly::ggplotly() interprets the grid system and translates it to html. (i.e. When I am using ggplotly, I trust that it will convert ggplot2 grid to html properly. ggplots are much easier to preview as static.)

By default, html ignores line breaks and multiple consecutive spaces.

# no line break
data.frame(
  x = c("x\ny")
) |> 
  gt()
 data.frame(x = c("x\ny") |> 
   gt() |>
  gt::opt_interactive()

image

grid is my new second favorite for easy previewing!

# line break
 data.frame(x = c("x\ny") |> 
   gt() |>
  as_gtable() |>
  plot()

image

Unfortunately, the output is not the same, hence a surprise output in my opinion.

We have to have one source of truth for this to be effective.

There are also a lot of contexts where \n is ignored in html and in many situations.

In md

Text
on the same line

In R,

cli::cli_abort("x\ny")
#> Error:
#> !  x y

In gt, by default.

data.frame(
  x = c("x\ny")
) |> 
  gt()

Changing \n as a linebreak in html would be a breaking change. I could live with it, but I think it would feel surprising for many JS / md /css / pandoc experts.

@rich-iannone feel free to add your grain of salt here!

One of my first use cases for as_gtable() would be to help create reprex (to proxy the html output), but it doesn't really work currently, because of the line break issue inconsistency.

There is also the danger of helpers like fmt_markdown(), gt(process_md), md(), html() that may not be respected everywhere, and then there is Quarto who may (or may not) post-process the table.

Edit: my assessment may have been incorrect. <br> works out of the box only for opt_interactive()

rich-iannone commented 1 month ago

Yeah, it might be a bit eccentric but I think <br> would be best for the reasons you outlined and more. Then, we'd have to have a number of examples and some explanations in the docs to explain this will be a cross-output method for introducing a hard line break.