rgtsummarygt

How to disable formatting of fractions in tbl_summary?


Fractions are being formatted within my tables and I do not know how to disable this formatting.

library(tidyverse)
library(gtsummary)


tibble(x= LETTERS[sample(1:5,24,replace=TRUE)], 
       y = rep(c("TEST1", "TEST 5/6"), 12)) |> 
  tbl_summary(by=y)

Table of example where label on first column shows a formatted fraction:

I want the label to be plain TEST 5/6.

I'm also doing this in VS code using radian as the console. Note this does not happen with every fraction, e.g.:

tibble(x= LETTERS[sample(1:5,24,replace=TRUE)], 
       y = rep(c("TEST 13/17", "TEST 5/6"), 12)) |> 
  tbl_summary(by=y)

Session Info:

sessionInfo()
R version 4.4.1 (2024-06-14 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 10 x64 (build 19045)

Matrix products: default


locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252 LC_NUMERIC=C                           LC_TIME=English_United States.1252

time zone: America/Chicago
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
 [1] gtsummary_2.0.3 lubridate_1.9.3 forcats_1.0.0   stringr_1.5.1   dplyr_1.1.4     purrr_1.0.2     readr_2.1.5     tidyr_1.3.1     tibble_3.2.1    ggplot2_3.5.1   tidyverse_2.0.0

loaded via a namespace (and not attached):
 [1] gtable_0.3.5      jsonlite_1.8.9    compiler_4.4.1    tidyselect_1.2.1  xml2_1.3.6        scales_1.3.0      fastmap_1.2.0     R6_2.5.1          commonmark_1.9.2  cards_0.3.0       generics_0.1.3    knitr_1.48        munsell_0.5.1     pillar_1.9.0
[15] tzdb_0.4.0        rlang_1.1.4       utf8_1.2.4        stringi_1.8.4     xfun_0.48         sass_0.4.9        timechange_0.3.0  cli_3.6.3         withr_3.0.2       magrittr_2.0.3    digest_0.6.37     grid_4.4.1        markdown_1.13     hms_1.1.3
[29] lifecycle_1.0.4   vctrs_0.6.5       glue_1.8.0        gt_0.11.1         fansi_1.0.6       colorspace_2.1-1  htmltools_0.5.8.1 tools_4.4.1       pkgconfig_2.0.3

Solution

  • The issue can be broken down to this:

    rstudio/markdown < 2.0

    > gt:::markdown_to_latex(text = "5/6", md_engine = "markdown")
    [1] "⅚"
    
    > packageVersion("markdown")
    [1] ‘1.13’
    

    "5/6" gets a special treatment during processing, see markdown:::pants for the relevant values (e.g. "6/7" works without special processing):

    > markdown:::pants
           1/2        1/3        2/3        1/4        3/4        1/5        2/5        3/5 
    "&frac12;" "&frac13;" "&frac23;" "&frac14;" "&frac34;" "&frac15;" "&frac25;" "&frac35;" 
           4/5        1/6        5/6        1/8        3/8        5/8        7/8        1/7 
    "&frac45;" "&frac16;" "&frac56;" "&frac18;" "&frac38;" "&frac58;" "&frac78;"  "&#8528;" 
           1/9       1/10        (c)        (r)       (tm) 
     "&#8529;"  "&#8530;"   "&copy;"    "&reg;"  "&trade;" 
    

    In markdown::markdown_options() is a smartypants option enabled which later causes the conversion which you see in the final table.

    What works for me in order to circumvent this is to use the HTML code for the forward slash:

    gt:::markdown_to_latex(text = "5&#47;6", md_engine = "markdown")
    [1] "5/6"
    

    enter image description here

    rstudio/markdown = 2.0

    Several users described that the issue described in the question is not reproducible. This is indeed the case with the markdown package in version 2.0.

    > gt:::markdown_to_latex(text = "5/6", md_engine = "markdown")
    [1] "5/6"
    
    > packageVersion("markdown")
    [1] ‘2.0’
    

    The reason is as follows: The markdown package is now superseded by litedown and therefore, several markdown functions are now only wrapper functions for litedown ones. In particular, markdown::markdown_options only calls litedown under the hood:

    > markdown::markdown_options
    function () 
    litedown::markdown_options()
    

    Now the special treatment of 5/6 is not seen anymore because litedown currently has no smartypants option

    # options disabled by default
    x2 = c(
      'toc', 'hardbreaks', 'tagfilter', 'number_sections', 'cleveref', 'offline',
      'smartypants'
    )
    

    (although litedown:::pants and litedown:::smartypants are available), whereas markdown up to 1.13 had:

    # options enabled by default
    x1 = c(
      'smart', 'smartypants', 'embed_resources', 'js_math', 'js_highlight',
      'superscript', 'subscript', 'latex_math', 'auto_identifiers',
      setdiff(commonmark::list_extensions(), 'tagfilter')
    )