rstudio/gt

text_transform and ampersands

Open

#2,033 opened on Sep 19, 2025

View on GitHub
 (2 comments) (0 reactions) (1 assignee)R (2,146 stars) (222 forks)batch import
Difficulty: [2] IntermediateEffort: [2] MediumFocus: HTML OutputGood First IssuePriority: [2] MediumType: ☹︎ Bug

Description

Description

Not sure if this is an issue with gt's text_transform() or with my code, but when I try to use text_transform() and stringr's str_to_title() convert strings containing ampersands into title case the ampersand is converted into the html character entity '&'.

Reproducible example

# Create tibble with strings containing ampersands
table_values <- tibble(
  pairs = c("MAC & CHEESE", "STRAWBERRY & VANILLA", "LEMON & GINGER")
)

# Create gt table and transform strings to title case
table_values |>
  gt() |>
  text_transform(
    fn = function(x) {
      str_to_title(x)
    },
    locations = cells_body(columns = pairs)
  )

# Note that the str_to_title() function on its own produces the correct result
str_to_title(table_values$pairs)

Expected result

The strings should be converted to title case while preserving the ampersands, as when the str_to_title() function is used outside of gt().

Session info

R version 4.4.1 (2024-06-14 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows Server 2016 x64 (build 14393)

Matrix products: default


locale:
[1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252   
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

time zone: Europe/London
tzcode source: internal

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

other attached packages:
 [1] gt_1.0.0        odbc_1.6.3      DBI_1.2.3       lubridate_1.9.4 forcats_1.0.0   stringr_1.5.2  
 [7] dplyr_1.1.4     purrr_1.1.0     readr_2.1.5     tidyr_1.3.1     tibble_3.3.0    ggplot2_4.0.0  
[13] tidyverse_2.0.0

loaded via a namespace (and not attached):
 [1] bit_4.6.0          gtable_0.3.6       compiler_4.4.1     Rcpp_1.1.0         tidyselect_1.2.1  
 [6] xml2_1.4.0         blob_1.2.4         scales_1.4.0       yaml_2.3.10        fastmap_1.2.0     
[11] R6_2.6.1           generics_0.1.4     knitr_1.50         pillar_1.11.0      RColorBrewer_1.1-3
[16] tzdb_0.5.0         rlang_1.1.6        utf8_1.2.6         stringi_1.8.7      xfun_0.53         
[21] sass_0.4.10        S7_0.2.0           pkgload_1.4.0      bit64_4.6.0-1      timechange_0.3.0  
[26] cli_3.6.4          withr_3.0.2        magrittr_2.0.4     digest_0.6.37      grid_4.4.1        
[31] rstudioapi_0.17.1  hms_1.1.3          lifecycle_1.0.4    vctrs_0.6.5        evaluate_1.0.5    
[36] glue_1.8.0         farver_2.1.2       rmarkdown_2.29     tools_4.4.1        pkgconfig_2.0.3   
[41] htmltools_0.5.8.1

Apologies if I'm just using text_transform() wrong!

Contributor guide

text_transform and ampersands · rstudio/gt#2033 | Good First Issue