Skip to content

Weird html output when using tab_style() with as_raw_html() #915

@Beck466

Description

@Beck466

Description

I am making a gt table which colours text in a row depending on the value in a column. gt behaves as expected when making the table, however when the table is used with as_raw_html the first cell of the first row is moved outside of the table (and other entries in that row are moved one to the left) if that row has been styled (not: works perfectly with gt 0.3.0 but not with gt 0.4.0

With gt 0.3.0:
image

With gt 0.4.0:
image

Reproducible example

library(tidyverse)
library(gt)

df <- tribble(~Site, ~Plant, ~Circuit, ~Frequency, ~Weeks, ~colour,
              "Site_1", "plant_1",  "circuit_1",    "Frequent", 28, "green",
              "Site_1", "plant_2",  "circuit_b",    "Frequent", 28, "green",
              "Site_1", "plant_3",  "circuit_c",    "Frequent", 28, "green",
              "Site_1", "plant_4",  "circuit_d",    "Frequent", 28, "green",
              "Site_1", "plant_5",  "circuit_e",    "Frequent", 26, "green",
              "Site_2", "plant_6",  "circuit_f",    "Frequent", 28, "black",
              "Site_2", "plant_7",  "circuit_g",    "Frequent", 28, "black",
              "Site_2", "plant_8",  "circuit_h",    "Frequent", 28, "green",
              "Site_2", "plant_9",  "circuit_i",    "Frequent", 28, "green",
              "Site_3", "plant_10", "circuit_j",    "Frequent", 28, "green",
              "Site_3", "plant_11", "circuit_k",    "Frequent", 28, "black",
              "Site_3", "plant_12", "circuit_l",    "Frequent", 28, "green",
              "Site_3", "plant_13", "circuit_m",    "Frequent", 28, "green")

df %>% 
  gt(groupname_col = "Site") %>% 
  tab_style(
    style = cell_text(weight = "bold"),
    locations = cells_column_labels(columns = everything())) %>% 
  tab_style(
    style = list(
      cell_text(weight = "bold"),
      cell_fill(color = "lightblue")),
    locations = cells_row_groups()) %>%
  tab_style(
    style = list(
      cell_text(color = "green", weight = "bold")
    ),
    locations = cells_body(
      rows = colour == "green")) %>%
  as_raw_html()
plant_1plant_10
Plant Circuit Frequency Weeks colour
Site_1
circuit_1 Frequent 28 green
plant_2 circuit_b Frequent 28 green
plant_3 circuit_c Frequent 28 green
plant_4 circuit_d Frequent 28 green
plant_5 circuit_e Frequent 26 green
Site_2
plant_6 circuit_f Frequent 28 black
plant_7 circuit_g Frequent 28 black
plant_8 circuit_h Frequent 28 green
plant_9 circuit_i Frequent 28 green
Site_3
circuit_j Frequent 28 green
plant_11 circuit_k Frequent 28 black
plant_12 circuit_l Frequent 28 green
plant_13 circuit_m Frequent 28 green

Created on 2022-03-24 by the reprex package (v2.0.1)

Session info
sessionInfo()
#> R version 3.6.3 (2020-02-29)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19043)
#> 
#> Matrix products: default
#> 
#> locale:
#> [1] LC_COLLATE=English_New Zealand.1252  LC_CTYPE=English_New Zealand.1252   
#> [3] LC_MONETARY=English_New Zealand.1252 LC_NUMERIC=C                        
#> [5] LC_TIME=English_New Zealand.1252    
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#>  [1] gt_0.4.0        forcats_0.5.1   stringr_1.4.0   dplyr_1.0.8    
#>  [5] purrr_0.3.4     readr_2.1.2     tidyr_1.2.0     tibble_3.1.6   
#>  [9] ggplot2_3.3.5   tidyverse_1.3.1
#> 
#> loaded via a namespace (and not attached):
#>  [1] Rcpp_1.0.8.3      lubridate_1.8.0   assertthat_0.2.1  digest_0.6.29    
#>  [5] utf8_1.2.2        R6_2.5.1          cellranger_1.1.0  backports_1.4.1  
#>  [9] reprex_2.0.1      evaluate_0.15     httr_1.4.2        highr_0.9        
#> [13] pillar_1.7.0      rlang_1.0.2       readxl_1.3.1      rstudioapi_0.13  
#> [17] R.utils_2.11.0    R.oo_1.24.0       checkmate_2.0.0   rmarkdown_2.13   
#> [21] styler_1.7.0      munsell_0.5.0     broom_0.7.12      compiler_3.6.3   
#> [25] modelr_0.1.8      xfun_0.30         pkgconfig_2.0.3   htmltools_0.5.2  
#> [29] tidyselect_1.1.2  fansi_1.0.2       crayon_1.5.0      tzdb_0.2.0       
#> [33] dbplyr_2.1.1      withr_2.5.0       rappdirs_0.3.3    R.methodsS3_1.8.1
#> [37] grid_3.6.3        jsonlite_1.8.0    gtable_0.3.0      lifecycle_1.0.1  
#> [41] DBI_1.1.2         magrittr_2.0.2    scales_1.1.1      cli_3.2.0        
#> [45] stringi_1.7.6     fs_1.5.2          xml2_1.3.3        ellipsis_0.3.2   
#> [49] generics_0.1.2    vctrs_0.3.8       tools_3.6.3       R.cache_0.15.0   
#> [53] glue_1.6.2        hms_1.1.1         fastmap_1.1.0     yaml_2.3.5       
#> [57] colorspace_2.0-3  rvest_1.0.2       knitr_1.37        haven_2.4.3      
#> [61] sass_0.4.1

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions