rstudio / gt

Easily generate information-rich, publication-quality tables from R
https://gt.rstudio.com
Other
2.02k stars 205 forks source link

cols_label and summary_rows combined makes gtsave output buggy #1233

Closed mafw closed 7 months ago

mafw commented 1 year ago

Description

If I relabel the columns in a table with summary rows using cols_label, the output from gtsave is wonky. If I output to .docx columns are shifted to the right. If i output to .rtf I get an error message from dplyr::select saying that the relabelled column doesn't exist.

Reproducible example

# Create a gt table using the `exibble` dataset
exibble_a <-
  exibble %>%
  select(-c(fctr, date, time, datetime)) %>%
  gt(rowname_col = "row", groupname_col = "group") %>%
  sub_missing()

# Create summary row
exibble_b <- 
  exibble_a %>%
  summary_rows(
    groups = TRUE,
    columns = num,
    fns = list(
      average = "mean",
      total = "sum",
      SD = "sd")
  ) 

# Relabel columns
exibble_c <- 
  exibble_b %>%
  cols_label(
    num = "Number"
  )

# Prints nicely
exibble_b

# Fails to save
gtsave(exibble_b, "exibble_b.rtf")

Session info

R version 4.1.2 (2021-11-01)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS 13.2.1

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

other attached packages:
 [1] forcats_0.5.1   stringr_1.4.0   dplyr_1.0.9    
 [4] purrr_0.3.4     readr_2.1.2     tidyr_1.2.0    
 [7] tibble_3.1.7    ggplot2_3.3.5   tidyverse_1.3.1
[10] gt_0.7.0       

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.8       cellranger_1.1.0 pillar_1.7.0    
 [4] compiler_4.1.2   dbplyr_2.1.1     tools_4.1.2     
 [7] digest_0.6.29    lubridate_1.8.0  jsonlite_1.7.3  
[10] lifecycle_1.0.1  gtable_0.3.0     pkgconfig_2.0.3 
[13] rlang_1.0.6      reprex_2.0.1     DBI_1.1.2       
[16] cli_3.4.1        rstudioapi_0.13  haven_2.4.3     
[19] fastmap_1.1.0    xml2_1.3.3       withr_2.5.0     
[22] httr_1.4.2       fs_1.5.2         hms_1.1.1       
[25] generics_0.1.2   vctrs_0.4.1      sass_0.4.1      
[28] grid_4.1.2       tidyselect_1.1.2 glue_1.6.2      
[31] R6_2.5.1         fansi_1.0.2      readxl_1.3.1    
[34] tzdb_0.2.0       modelr_0.1.8     magrittr_2.0.2  
[37] scales_1.2.1     backports_1.4.1  ellipsis_0.3.2  
[40] htmltools_0.5.2  rvest_1.0.2      assertthat_0.2.1
[43] colorspace_2.0-2 utf8_1.2.2       stringi_1.7.6   
[46] munsell_0.5.0    broom_0.7.12     crayon_1.4.2   
olivroy commented 8 months ago

@rich-iannone . I can't reproduce this issue now with gt 0.10.1, R 4.3.2, latest RStudio, Windows.

Edit: there is a small typo in the reprex. (should not have 'groups = TRUE`).

But I can't reproduce the error and the output of gtsave(exibble_c, "exibble_c.docx") is fine.

docx image rtf

rich-iannone commented 8 months ago

Thank you @olivroy for the repro testing here. Must have been fixed somewhere down the line. Given everything is good here, I will close the issue.

olivroy commented 8 months ago

Oops. Mistake @rich-iannone, there are 2 typos in the example that prevent actually correctly generating the error.

# Create a gt table using the `exibble` dataset
library(gt)
library(dplyr)
exibble_a <-
  exibble %>%
  select(-c(fctr, date, time, datetime)) %>%
  gt::gt(rowname_col = "row", groupname_col = "group") %>%
  sub_missing()

# Create summary row
exibble_b <- 
  exibble_a %>%
  summary_rows(
    #groups = TRUE, # typo in the reprex 
    columns = num,
    fns = list(
      average = "mean",
      total = "sum",
      SD = "sd")
  ) 

# Relabel columns
exibble_c <- 
  exibble_b %>%
  cols_label(
    num = "Number"
  )

# Prints nicely
exibble_c

# Fails to save
gtsave(exibble_c, "exibble_c.rtf") # typos in the reprex. 
#> Error in `dplyr::all_of()`:
#> ! Can't subset columns that don't exist.
#> ✖ Column `Number` doesn't exist.

Created on 2024-02-22 with reprex v2.1.0

Probably an easy fix to provide a named vector to all_of().