Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fmt_markdown() doesnt affect group cols #1579

Open
Tracked by #1793
joeycouse opened this issue Feb 14, 2024 · 1 comment
Open
Tracked by #1793

fmt_markdown() doesnt affect group cols #1579

joeycouse opened this issue Feb 14, 2024 · 1 comment

Comments

@joeycouse
Copy link

fmt_markdown() does not affect group cols when attempting to format with markdown.

library(gt)
suppressMessages(library(dplyr))

gtcars |> 
  mutate(group_col = paste0("<b>", ctry_origin, ' ', mfr, "</b>"),
         mfr = paste0("<b>",mfr,"</b>")) |> 
  head(10) |> 
  gt(groupname_col = "group_col",
    row_group_as_column = T) |> 
  fmt_markdown(columns = "group_col") |> 
  fmt_markdown(columns = "mfr")
mfr model year trim bdy_style hp hp_rpm trq trq_rpm mpg_c mpg_h drivetrain trsmn ctry_origin msrp
<b>United States Ford</b>

Ford

GT 2017 Base Coupe coupe 647 6250 550 5900 11 18 rwd 7a United States 447000
<b>Italy Ferrari</b>

Ferrari

458 Speciale 2015 Base Coupe coupe 597 9000 398 6000 13 17 rwd 7a Italy 291744

Ferrari

458 Spider 2015 Base convertible 562 9000 398 6000 13 17 rwd 7a Italy 263553

Ferrari

458 Italia 2014 Base Coupe coupe 562 9000 398 6000 13 17 rwd 7a Italy 233509

Ferrari

488 GTB 2016 Base Coupe coupe 661 8000 561 3000 15 22 rwd 7a Italy 245400

Ferrari

California 2015 Base Convertible convertible 553 7500 557 4750 16 23 rwd 7a Italy 198973

Ferrari

GTC4Lusso 2017 Base Coupe coupe 680 8250 514 5750 12 17 awd 7a Italy 298000

Ferrari

FF 2015 Base Coupe coupe 652 8000 504 6000 11 16 awd 7a Italy 295000

Ferrari

F12Berlinetta 2015 Base Coupe coupe 731 8250 509 6000 11 16 rwd 7a Italy 319995

Ferrari

LaFerrari 2015 Base Coupe coupe 949 9000 664 6750 12 16 rwd 7a Italy 1416362
sessionInfo()
#> R version 4.3.2 (2023-10-31)
#> Platform: aarch64-apple-darwin20 (64-bit)
#> Running under: macOS Sonoma 14.2.1
#> 
#> Matrix products: default
#> BLAS:   /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRblas.0.dylib 
#> LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0
#> 
#> locale:
#> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#> 
#> time zone: America/Chicago
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] dplyr_1.1.4 gt_0.10.1  
#> 
#> loaded via a namespace (and not attached):
#>  [1] vctrs_0.6.5       cli_3.6.2         knitr_1.45        rlang_1.1.2      
#>  [5] xfun_0.41         generics_0.1.3    glue_1.7.0        markdown_1.12    
#>  [9] htmltools_0.5.7   sass_0.4.8        fansi_1.0.6       rmarkdown_2.25   
#> [13] evaluate_0.23     tibble_3.2.1      fastmap_1.1.1     yaml_2.3.8       
#> [17] lifecycle_1.0.4   compiler_4.3.2    fs_1.6.3          pkgconfig_2.0.3  
#> [21] rstudioapi_0.15.0 digest_0.6.33     R6_2.5.1          reprex_2.0.2     
#> [25] tidyselect_1.2.0  utf8_1.2.4        pillar_1.9.0      commonmark_1.9.1 
#> [29] magrittr_2.0.3    tools_4.3.2       withr_2.5.2       xml2_1.3.6

Created on 2024-02-14 with reprex v2.0.2

@rich-iannone rich-iannone moved this from Backlog to Next / Ready for Dev in R Markdown Team Projects Feb 14, 2024
@rich-iannone rich-iannone added this to the v0.10.2 milestone Feb 14, 2024
@joeycouse joeycouse changed the title fmt_markdown() does affect group cols fmt_markdown() doesnt affect group cols Feb 15, 2024
@rich-iannone rich-iannone modified the milestones: v0.10.2, v0.11.0, v0.12.0 Jun 6, 2024
@olivroy
Copy link
Collaborator

olivroy commented Oct 2, 2024

A workaround for now is the following:

Basically, at render stage, the formats are not consulted when rendering row groups.

A workaround is to use process_md = TRUE in the gt() call:

library(gt)
suppressMessages(library(dplyr))

gtcars |> 
  mutate(group_col = paste0("<b>", ctry_origin, ' ', mfr, "</b>"),
         mfr = paste0("<b>",mfr,"</b>")) |> 
  head(10) |> 
  gt(groupname_col = "group_col", process_md = TRUE,
    row_group_as_column = T) 

Since formats are rendered at the last stage, it may require a larger change.

gt/R/dt_stub_df.R

Lines 120 to 137 in 10711c5

if (process_md) {
stub_df$group_label <-
lapply(
seq_along(row_group_labels),
FUN = function(x) md(row_group_labels[x])
)
} else {
stub_df[["group_label"]] <- as.list(row_group_labels)
}
data <- dt_boxhead_set_row_group(data = data, vars = groupname_col)
}
# Stop if input `data` has no columns (after modifying `data` for groups)
if (ncol(data_tbl) == 0L) {
cli::cli_abort(

Also, if you are looking to bold things, you may want to take a look at tab_options() / `tab_style().

The following should be more robust code:

gtcars |> 
  mutate(group_col = paste(ctry_origin, mfr)) |> 
  head(10) |> 
  gt(groupname_col = "group_col",
    row_group_as_column = T) |>
  tab_style(
      cell_text(weight = "bold"), 
      cells_body(columns = mfr)
  ) |>
  tab_options(row_group.font.westubight = "bold")

A more accurate title would be : formats don't affect group cols and row names (stub)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Next / Ready for Dev
Development

No branches or pull requests

3 participants