Skip to content

gt::fmt_markdown() strips numeric formatting and line breaks from merged cells #1765

Closed as not planned

Description

@ddsjoberg

Prework

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()

image

# 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()

image

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!! 🫶

Activity

  1. olivroy commented on Jul 10, 2024

    @olivroy
    Collaborator

    You can use <br> and it will work in both examples. I don't know why \n works in one, but not the other, but technically, it shouldn't work in either places.

    markdown::mark_html("x\ny") %>% writeLines("x.html")
    browseURL("x.html")

    image

    # gives the expected result. (EDITED)
    markdown::mark_html("x <br> y") %>% writeLines("x.html")

    I am no expert however!

    As for the fmt_number() call, it is an issue that fmt_markdown() breaks fmt_number.

    Meanwhile, I'd highly recommend you using

    # 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}  <br> ({2})")

    which gives the correct result.
    image

  2. ddsjoberg commented on Jul 10, 2024

    @ddsjoberg
    ContributorAuthor

    Thank you @olivroy ! I wasn't aware that \n isn'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 the cols_merge(rows) argument a while back, I was updating my code to use it which is when I encountered this issue 🤔

  3. ddsjoberg commented on Jul 10, 2024

    @ddsjoberg
    ContributorAuthor

    Is there a line breaker that works across output types?

  4. olivroy commented on Jul 10, 2024

    @olivroy
    Collaborator

    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
    .

    # 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}  <br> ({2})")

    The secret may lay in the usage of html() or md() where needed to let parsers know we are using md and / or html().

    And may I ask if this feature worked in gt 0.10?

  5. ddsjoberg commented on Jul 10, 2024

    @ddsjoberg
    ContributorAuthor

    And may I ask if this feature worked in gt 0.10?

    I don't think so. I was just updating my function to use gt::cols_merge() since the function now has the rows argument. Previously my code was doing a manual merge by creating character columns to paste together, and I was doing this because there was no rows argument in cols_merge(). I've had an open issue for a few years to switch to gt::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).

  6. rich-iannone commented on Jul 10, 2024

    @rich-iannone
    Member

    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:

    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()

    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.

  7. ddsjoberg commented on Jul 10, 2024

    @ddsjoberg
    ContributorAuthor

    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!

  8. olivroy commented on Jul 11, 2024

    @olivroy
    Collaborator

    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

  9. ddsjoberg commented on Jul 11, 2024

    @ddsjoberg
    ContributorAuthor

    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?

  10. olivroy commented on Jul 11, 2024

    @olivroy
    Collaborator

    @ddsjoberg I should have linked it #1767!

  11. ddsjoberg commented on Jul 11, 2024

    @ddsjoberg
    ContributorAuthor

    One line breaker to rule them all! Thanks!

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions