Repository navigation
Closed as not planned
Closed as not planned
Copy link
Description
Prework
- Read and agree to the code of conduct and contributing guidelines.
- If there is already a relevant issue, whether open or closed, comment on the existing thread instead of posting a new issue.
Description
Describe the bug clearly and concisely.
Reproducible example
packageVersion("gt")
#> [1] '0.11.0'
# using `\n` in a cell for a line break, which requires `gt::fmt_markdown()`
gt1 <- data.frame(estimate = "123 \n(0.1234)") |>
gt::gt() |>
gt::fmt_markdown()# combining numeric formatting, merging, and the line break does not work
gt2 <- data.frame(estimate = pi, std.error = pi / 10) |>
gt::gt() |>
gt::fmt_number() |>
gt::cols_merge(columns = c(estimate, std.error), pattern = "{1} \n({2})") |>
gt::fmt_markdown()Created on 2024-07-10 with reprex v2.1.1
Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#> setting value
#> version R version 4.4.0 (2024-04-24)
#> os macOS Ventura 13.4.1
#> system aarch64, darwin20
#> ui X11
#> language (EN)
#> collate en_US.UTF-8
#> ctype en_US.UTF-8
#> tz America/Los_Angeles
#> date 2024-07-10
#> pandoc 3.1.11 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/aarch64/ (via rmarkdown)
#>
#> ─ Packages ───────────────────────────────────────────────────────────────────
#> package * version date (UTC) lib source
#> cli 3.6.3 2024-06-21 [1] CRAN (R 4.4.0)
#> digest 0.6.36 2024-06-23 [1] CRAN (R 4.4.0)
#> dplyr 1.1.4 2023-11-17 [1] CRAN (R 4.4.0)
#> evaluate 0.24.0 2024-06-10 [1] CRAN (R 4.4.0)
#> fansi 1.0.6 2023-12-08 [1] CRAN (R 4.4.0)
#> fastmap 1.2.0 2024-05-15 [1] CRAN (R 4.4.0)
#> fs 1.6.4 2024-04-25 [1] CRAN (R 4.4.0)
#> generics 0.1.3 2022-07-05 [1] CRAN (R 4.4.0)
#> glue 1.7.0 2024-01-09 [1] CRAN (R 4.4.0)
#> gt 0.11.0 2024-07-09 [1] CRAN (R 4.4.0)
#> htmltools 0.5.8.1 2024-04-04 [1] CRAN (R 4.4.0)
#> knitr 1.48 2024-07-07 [1] CRAN (R 4.4.0)
#> lifecycle 1.0.4 2023-11-07 [1] CRAN (R 4.4.0)
#> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.4.0)
#> pillar 1.9.0 2023-03-22 [1] CRAN (R 4.4.0)
#> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.4.0)
#> R6 2.5.1 2021-08-19 [1] CRAN (R 4.4.0)
#> reprex 2.1.1 2024-07-06 [1] CRAN (R 4.4.0)
#> rlang 1.1.4 2024-06-04 [1] CRAN (R 4.4.0)
#> rmarkdown 2.27 2024-05-17 [1] CRAN (R 4.4.0)
#> rstudioapi 0.16.0 2024-03-24 [1] CRAN (R 4.4.0)
#> sessioninfo 1.2.2 2021-12-06 [2] CRAN (R 4.4.0)
#> tibble 3.2.1 2023-03-20 [1] CRAN (R 4.4.0)
#> tidyselect 1.2.1 2024-03-11 [1] CRAN (R 4.4.0)
#> utf8 1.2.4 2023-10-22 [1] CRAN (R 4.4.0)
#> vctrs 0.6.5 2023-12-01 [1] CRAN (R 4.4.0)
#> withr 3.0.0 2024-01-16 [1] CRAN (R 4.4.0)
#> xfun 0.45 2024-06-16 [1] CRAN (R 4.4.0)
#> xml2 1.3.6 2023-12-04 [1] CRAN (R 4.4.0)
#> yaml 2.3.9 2024-07-05 [1] CRAN (R 4.4.0)
#>
#> [1] /Users/sjobergd/Library/R/arm64/4.4/library
#> [2] /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library
#>
#> ──────────────────────────────────────────────────────────────────────────────Expected result
In the second example, I would like to see the numeric formatting applied to the columns, while including the line break in the cell.
Many finance, economic, and psychometric journals require that regression model results be presented with the coeficient above the standard error. That is where this is coming from. FYI
Thank you for the amazing work on the package!! 🫶
Metadata
Metadata
Assignees
Labels
No labels


Activity
olivroy commentedon Jul 10, 2024
You can use
<br>and it will work in both examples. I don't know why\nworks in one, but not the other, but technically, it shouldn't work in either places.I am no expert however!
As for the
fmt_number()call, it is an issue thatfmt_markdown()breaks fmt_number.Meanwhile, I'd highly recommend you using
which gives the correct result.

ddsjoberg commentedon Jul 10, 2024
Thank you @olivroy ! I wasn't aware that
\nisn't supposed to work necessarily 😆In this case, I need the line break to work even when the output isn't HTML. For a bit of background, I maintain a package called gtsummary that summarizes statistical results for researchers, and the users output their results to many different output types using gt.
In the past, I've done a manual column merge, where I apply the formatting functions to create character columns that I paste together. In those cases, I was using the
"\n"for the line break in the cell of the character column. Since gt added thecols_merge(rows)argument a while back, I was updating my code to use it which is when I encountered this issue 🤔ddsjoberg commentedon Jul 10, 2024
Is there a line breaker that works across output types?
olivroy commentedon Jul 10, 2024
I am not sure! Still trying to figure it out!
The new grid support doesn't work with
word output seems to ignore both \n and
.
The secret may lay in the usage of
html()ormd()where needed to let parsers know we are using md and / orhtml().And may I ask if this feature worked in gt 0.10?
ddsjoberg commentedon Jul 10, 2024
I don't think so. I was just updating my function to use
gt::cols_merge()since the function now has therowsargument. Previously my code was doing a manual merge by creating character columns to paste together, and I was doing this because there was norowsargument incols_merge(). I've had an open issue for a few years to switch togt::cols_merge()once the rows arg was added, since many users like to continue formatting the columns after converting the object to gt (which hasn't been possible, because i was already converting to character).rich-iannone commentedon Jul 10, 2024
Two formatters called on the same cell will always result in the situation where the last formatter wins. So
fmt_number()has no effect in the code: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 callfmt_markdown(). This could probably be done in theprocess_text()function.We incidentally also need a similar bit of 'extra' markup to represent cross-output tab stops but that's for another issue.
ddsjoberg commentedon Jul 10, 2024
ahhh, that makes sense about two
fmt_*()being called on the same cell. That's the exact behaviour I would expect if I wanted to change the numeric formatting, for example!olivroy commentedon Jul 11, 2024
Since the real issue (have a universal linebreak across formats) is unrelated, I am going to close this one! I opened #1767. Happy to see more contributions from you @ddsjoberg and excited to see the new version of gtsummary
ddsjoberg commentedon Jul 11, 2024
Thanks for the convo and helping me understand @olivroy and @rich-iannone !
Should I open a separate issue about a line breaker that is supported for all output formats?
olivroy commentedon Jul 11, 2024
@ddsjoberg I should have linked it #1767!
ddsjoberg commentedon Jul 11, 2024
One line breaker to rule them all! Thanks!