Skip to content

Setting cols_width causes column to "disappear" #1510

Closed

Description

@schwa021

Description

I can set the column widths of all but one column. When I set the last column width a column disappears from the table. The reprex and images below will make this more understandable.

Reproducible example

I have the following tibble named tdat that has columns (Model, name, err_Treated, ..., coverage_Control)

Model, name, err_Treated, err_Control, width90_Treated, width90_Control, coverage_Treated, coverage_Control
Match, A, 12, 8, 5 ,36, 0.91, 0.82
Match, B, 5, 6, 29, 25, 0.87, 0.81
Match, C, 10, 9, 42, 41, 0.79, 0.82
BART, A, 7, 7, 37, 37, 0.89, 0.87
BART, B, 6, 5, 26, 26, 0.87, 0.9
BART, C, 9, 9, 49, 48, 0.93 ,0.87

(sorry - I don't know of a better way to reprex this)

I use the following code to create and format a gt() table:

 tbl_cover <-
   tdat |>
   gt(groupname_col = "name") |>
   cols_label(
     err_Treated = "Treated",
     err_Control = "Control",
     width90_Treated = "Treated",
     width90_Control = "Control",
     coverage_Treated = "Treated",
     coverage_Control = "Control"
   ) |>
   fmt_percent(columns = starts_with("cover"), decimals = 0) |>
   # tab_header(str_replace_all(s, "_", " ")) |>
   tab_style(
     cell_text(weight = "bold", align = "left"),
     locations = list(cells_row_groups(), cells_title())
   ) |>
   tab_style(cell_text(align = "center"),
             locations = list(
               cells_body(columns = everything()),
               cells_column_labels(columns = everything())
             )) |>
   tab_style(cell_text(style = "italic"),
             locations = list(
               cells_column_labels(columns = matches("width|cover|err")),
               cells_body(columns = Model)
             )) |>
   tab_style(cell_text(align = "left", indent = px(25)),
             locations = cells_body(columns = Model)) |>
   tab_style(cell_text(color = "transparent"),
             locations = cells_column_labels(columns = Model)) |>
   cols_width(
     # err_Treated ~ px(100),
     err_Control ~ px(100),
     starts_with("cover") ~ px(100),
     starts_with("width") ~ px(100),
     Model ~ px(230)
   ) |>
   tab_spanner(label = "Mean Abs. Error",
               columns = starts_with("err")) |>
   tab_spanner(label = "Interval Width",
               columns = starts_with("width")) |>
   tab_spanner(label = "Coverage",
               columns = starts_with("cover")) |>
   tab_style(cell_text(weight = "bold"),
             locations = cells_column_spanners()) |>
   tab_options(table_body.hlines.color = "#e6e6e6",
               row_group.as_column = TRUE)

Created on 2023-12-17 with reprex v2.0.2

This produces the expected table:
image

Now I uncomment the line: # err_Treated ~ px(100). In other words, I now assign a width to that final column. Note that it doesn't matter which column is chosen as the last column to assign a width to. The point is that if I assign widths to all-but-one, it works. But when I assign the final width, my last column disappears.

Expected result

I expected to see a table almost identical to the one posted above, but with the err_Treated column 100 pixels wide.

Instead, the last column of the table disappears (see below):

image

Session info

sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.2.2 (2022-10-31 ucrt)
#>  os       Windows 10 x64 (build 19045)
#>  system   x86_64, mingw32
#>  ui       RTerm
#>  language (EN)
#>  collate  English_United States.utf8
#>  ctype    English_United States.utf8
#>  tz       America/Chicago
#>  date     2023-12-17
#>  pandoc   3.1.1 @ C:/Users/mschwartz/AppData/Local/Programs/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version    date (UTC) lib source
#>  cli           3.6.1      2023-03-23 [1] CRAN (R 4.2.3)
#>  digest        0.6.33     2023-07-07 [1] CRAN (R 4.2.3)
#>  evaluate      0.23       2023-11-01 [1] CRAN (R 4.2.3)
#>  fastmap       1.1.1      2023-02-24 [1] CRAN (R 4.2.3)
#>  fs            1.6.3      2023-07-20 [1] CRAN (R 4.2.3)
#>  glue          1.6.2      2022-02-24 [1] CRAN (R 4.2.3)
#>  htmltools     0.5.7.9000 2023-11-05 [1] Github (rstudio/htmltools@7efa2d8)
#>  knitr         1.45       2023-10-30 [1] CRAN (R 4.2.3)
#>  lifecycle     1.0.3      2022-10-07 [1] CRAN (R 4.2.3)
#>  reprex        2.0.2      2022-08-17 [1] CRAN (R 4.2.3)
#>  rlang         1.1.1      2023-04-28 [1] CRAN (R 4.2.3)
#>  rmarkdown     2.25       2023-09-18 [1] CRAN (R 4.2.3)
#>  rstudioapi    0.15.0     2023-07-07 [1] CRAN (R 4.2.3)
#>  sessioninfo   1.2.2      2021-12-06 [1] CRAN (R 4.2.3)
#>  withr         2.5.2      2023-10-30 [1] CRAN (R 4.2.3)
#>  xfun          0.41       2023-11-01 [1] CRAN (R 4.2.3)
#>  yaml          2.3.7      2023-01-23 [1] CRAN (R 4.2.3)
#> 
#>  [1] C:/Users/mschwartz/AppData/Local/R/win-library/4.2
#>  [2] C:/Program Files/R/R-4.2.2/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

Activity

  1. rich-iannone commented on Dec 30, 2023

    @rich-iannone
    Member

    Thanks for filing this issue (and for the example). This is definitely enough to go on and a fix will be implemented soon!

  2. added this to the v0.10.1 milestone on Dec 30, 2023
  3. moved this from Backlog to Todo In Progress in R Markdown Team Projectson Dec 30, 2023
  4. modified the milestones: v0.10.1, v0.10.2 on Jan 17, 2024
  5. moved this from Todo In Progress to Next / Ready for Dev in R Markdown Team Projectson May 29, 2024
  6. olivroy commented on Jun 14, 2024

    @olivroy
    Collaborator

    Minimal reprex

    # doesn't work
    library(gt)
    tb <- mtcars %>% 
      dplyr::select(mpg, cyl, vs) %>% 
      head() %>% 
      gt(groupname_col = "vs", row_group_as_column = T)
    # col disappears
    tb %>% 
      cols_width(
        mpg ~ px(100),
        cyl ~ px(100),
      )
    
    # works
    tb %>% 
      cols_width(
        cyl ~ px(100),
      )

    image

  7. modified the milestones: v0.12.0, v0.11.0 on Jul 3, 2024
  8. moved this from Next / Ready for Dev to Done in R Markdown Team Projectson Jul 3, 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

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions