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

docx table width #1907

Open
Liripo opened this issue Oct 15, 2024 · 2 comments
Open

docx table width #1907

Liripo opened this issue Oct 15, 2024 · 2 comments

Comments

@Liripo
Copy link

Liripo commented Oct 15, 2024

---
title: "docx_gt"
format: 
  docx:
    toc: true
    number-sections: true
    toc-title: 目录
    toc-depth: 3
editor: visual
---

## Quarto

```{r}
library(gt)
gt(head(mtcars)) |>
    tab_options(table.width = pct(100), column_labels.font.weight = "bold") |>
    cols_align(align = "left")


gt(head(mtcars)) |>
    tab_options(table.width = px(600), column_labels.font.weight = "bold") |>
    cols_align(align = "left")
```

The above code does not take effect on the table width. How to adjust the table width?

@barrettk
Copy link

barrettk commented Dec 4, 2024

Just to add some more details, this also doesnt seem to work outside of an Rmarkdown or quarto context either:

Rendered in HTML

Starting table

gt_table

Image

With requested width

gt_table %>% gt::tab_options(table.width = gt::pct(100)) 

Image

Saving out to MS Word

tmp_doc <- file.path(tempdir(), "tmp.docx")

gt_table %>%
  gt::tab_options(table.width = gt::pct(100)) %>%  # Also tried with `gt::px(700)`
  gt::gtsave(gt_table, filename = basename(tmp_doc), path = dirname(tmp_doc))

# Also tried:
gt_table %>% gt::cols_width(everything() ~ gt::pct(100))

Image

Note that the additional borders and loss of other formatting is likely related to #1098 (not worried about that for now, but just mentioning)

Environment details
> packageVersion("gt")
[1] ‘0.10.1> sessionInfo()
R version 4.1.3 (2022-03-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.5 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/atlas/libblas.so.3.10.3
LAPACK: /usr/lib/x86_64-linux-gnu/atlas/liblapack.so.3.10.3

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
 [1] officer_0.6.6     mrggsave_0.4.5    data.table_1.15.4 yspec_0.6.2       gt_0.10.1         glue_1.7.0        pmparams_0.2.0   
 [8] yaml_2.3.8        magrittr_2.0.3    bbr_1.11.0        here_1.0.1        pmtables_0.8.0    lubridate_1.9.3   forcats_1.0.0    
[15] 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    
[22] tidyverse_2.0.0  

loaded via a namespace (and not attached):
 [1] fs_1.6.4                fontquiver_0.2.1        bit64_4.0.5             RColorBrewer_1.1-3      rprojroot_2.0.4        
 [6] tools_4.1.3             backports_1.5.0         utf8_1.2.4              R6_2.5.1                colorspace_2.1-0       
[11] withr_3.0.0             tidyselect_1.2.1        gridExtra_2.3           processx_3.8.4          bit_4.0.5              
[16] curl_5.2.1              compiler_4.1.3          textshaping_0.3.7       cli_3.6.2               flextable_0.9.6        
[21] xml2_1.3.6              fontBitstreamVera_0.1.1 sass_0.4.9              scales_1.3.0            checkmate_2.3.1        
[26] callr_3.7.6             askpass_1.2.0           commonmark_1.9.1        systemfonts_1.1.0       digest_0.6.35          
[31] rmarkdown_2.27          gfonts_0.2.0            pkgconfig_2.0.3         htmltools_0.5.8.1       fastmap_1.2.0          
[36] rlang_1.1.4             httpcode_0.3.0          rstudioapi_0.16.0       shiny_1.8.1.1           gridGraphics_0.5-1     
[41] generics_0.1.3          jsonlite_1.8.8          nmrec_0.4.0             vroom_1.6.5             zip_2.3.1              
[46] Rcpp_1.0.12             munsell_0.5.1           fansi_1.0.6             gdtools_0.3.7           lifecycle_1.0.4        
[51] stringi_1.8.4           grid_4.1.3              promises_1.3.0          crayon_1.5.2            hms_1.1.3              
[56] magick_2.8.3            knitr_1.47              ps_1.7.6                pillar_1.9.0            uuid_1.2-0             
[61] markdown_1.13           crul_1.4.2              evaluate_0.24.0         pdftools_3.4.0          qpdf_1.3.3             
[66] fontLiberation_0.1.0    renv_0.15.5             httpuv_1.6.15           vctrs_0.6.5             tzdb_0.4.0             
[71] gtable_0.3.5            openssl_2.2.0           assertthat_0.2.1        xfun_0.44               mime_0.12              
[76] xtable_1.8-4            later_1.3.2             ragg_1.3.2              diffobj_0.3.5           timechange_0.3.0

@elfunesto
Copy link

Dear all,
Looking at the function create_table_props_component_xmlwhich is called when rendering in docx, I see that table_width is recovered but not used in the return htmltags, I wonder whether that might be the reason? table_properties does not have any info on table width

function (data, align = c("center", "start", "end", "right", 
  "left")) 
{
  align <- match.arg(align)
  boxh <- dt_boxhead_get(data = data)
  table_style <- paste0("table-layout: ", dt_options_get_value(data = data, 
    option = "table_layout"), ";")
  doc_page_width <- getOption("gt.rtf_page_width")
  table_width <- dt_options_get_value(data = data, option = "table_width")
  widths <- boxh[boxh$type %in% c("default", "stub"), , drop = FALSE]
  widths <- dplyr::arrange(widths, dplyr::desc(type))$column_width
  table_properties <- xml_tblPr(xml_tbl_cell_margins(xml_width("top"), 
    xml_width("bottom"), xml_width("left", width = 60), 
    xml_width("right", width = 60)), xml_tblW(type = "auto", 
    w = 0), xml_tblLook(), xml_jc(val = c(center = "center", 
    start = "start", end = "end", end = "right", start = "left")[[align]]))
  htmltools::tagList(table_properties)
}

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

No branches or pull requests

3 participants