Skip to content

cols_width specifications and as_gtable #1771

Closed

Description

@srcm-mark

Prework

Description

Hello, all:

Thanks so very much for the as_gtable update. As I was converting code to take advantage of this new feature, I have found that the as_gtable step seems to break on a table that includes a cols_width specification using pct(). The error arises when setting a single column to a value < 100, or multiple columns to values <= 100.

Reproducible example

library(gt)

start_date <- "2010-06-07"
end_date <- "2010-06-08"

sp_tab <- 
  sp500 |>
  dplyr::filter(date >= start_date & date <= end_date) |>
  gt() |>
  cols_width(date ~ pct(50))

sp_tab |> as_gtable()
#> Warning in parse_px_to_pt(column_width[fixed]): NAs introduced by coercion
#> Error in if (extra_width < 0) {: missing value where TRUE/FALSE needed

Created on 2024-07-11 with reprex v2.1.1

Session info
sessionInfo()
#> R version 4.3.3 (2024-02-29 ucrt)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows Server 2019 x64 (build 19045)
#> 
#> Matrix products: default
#> 
#> 
#> locale:
#> [1] LC_COLLATE=English_United States.utf8 
#> [2] LC_CTYPE=English_United States.utf8   
#> [3] LC_MONETARY=English_United States.utf8
#> [4] LC_NUMERIC=C                          
#> [5] LC_TIME=English_United States.utf8    
#> 
#> time zone: America/Los_Angeles
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] gt_0.11.0
#> 
#> loaded via a namespace (and not attached):
#>  [1] vctrs_0.6.5       cli_3.6.3         knitr_1.48        rlang_1.1.4      
#>  [5] xfun_0.45         generics_0.1.3    glue_1.7.0        htmltools_0.5.8.1
#>  [9] sass_0.4.9        fansi_1.0.6       rmarkdown_2.27    grid_4.3.3       
#> [13] evaluate_0.24.0   tibble_3.2.1      fastmap_1.2.0     yaml_2.3.9       
#> [17] lifecycle_1.0.4   compiler_4.3.3    dplyr_1.1.4       fs_1.6.4         
#> [21] pkgconfig_2.0.3   rstudioapi_0.16.0 digest_0.6.36     R6_2.5.1         
#> [25] reprex_2.1.1      tidyselect_1.2.1  utf8_1.2.4        pillar_1.9.0     
#> [29] magrittr_2.0.3    gtable_0.3.5      tools_4.3.3       withr_3.0.0      
#> [33] xml2_1.3.6

Expected result

I would expect the result to reflect specific column width assignment prior to output via as_gtable. Without the cols_width set, tables output as expected using as_gtable. With cols_width set, none does.

Activity

  1. olivroy commented on Jul 11, 2024

    @olivroy
    Collaborator

    Thanks for the report!

    @teunbrand Do you know how we could convert percentage to points? Basically, the error is caused by the fact that the conversion from px to pt occurs here. And since the percentage stays there in the gsub() call of parse_px_to_pt(), then the width becomes NA, which is causing the issue here.

    gt/R/export.R

    Line 1446 in 8295458

    widths[fixed] <- pmax(parse_px_to_pt(column_width[fixed]), widths[fixed])

  2. teunbrand commented on Jul 11, 2024

    @teunbrand
    Contributor

    Percentages shouldn't be converted to points, they should be converted to 'npc' units. It is probably a small oversight on my part that I didn't realise columns widths could be percentages. My initial hunch is that percentage widths should be excluded in the fixed variable, but I'll take a more detailed look soon.

  3. olivroy commented on Jul 11, 2024

    @olivroy
    Collaborator

    Thanks! This would be great.

    There is also another thing I noticed that the second grid::unit() call has a missing unit here: (I see the missing argument linter)

    gt/R/export.R

    Line 1417 in 8295458

    right <- grid::unit(grid::unit(parse_px_to_pt(left), "pt"))

  4. added this to the v0.12.0 milestone on Jul 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions