jthomasmock / gtExtras

A Collection of Helper Functions for the gt Package.
https://jthomasmock.github.io/gtExtras/
Other
195 stars 27 forks source link

gt_plt_bar() feature disrupts Rmarkdown knitting when in a for loop or result as a function #91

Closed ait5 closed 1 year ago

ait5 commented 1 year ago

Description

Using gt() function to generate tables in a report file with Rmarkdown in Rstudio (knitting to HTML).

ISSUE: When adding gt_plt_bar() to show a bar chart inside the table, these resulting tables can only be knitted in the Rmarkdown file while not in a for loop. Something along the HTML code gets disrupted since there is some printing HTML code happening when failing to knit (see section "_Normal table WITH gt_pltbar() inside a for loop" of the example below).

Reproducible example

Here is a reproducible example for Rmarkdown (you can copy&paste the whole code into your own Rmarkdown).


title: "gt_plt_bar_ISSUE" output: html_document date: "2023-05-18"

knitr::opts_chunk$set(
  echo = FALSE,
  error = FALSE, 
  warning = FALSE, 
  message = FALSE, 
  cache = FALSE
)

library(dplyr)
library(gt)
library(gtExtras)

Test for gt_plt_bar() issue

# Create dummy data frame
dummy_counts <- data.frame(
  allele_value = c("A", "B", "C"),
  Overall = c(10, 15, 20),
  text_Het = c(5, 8, 12),
  text_Hom = c(5, 7, 8)
)
my_gt_table <- function(data){
  data %>%
    gt() %>%
    # Add barchart
    gt_plt_bar( column = Overall, color = "black",
                scale_type = "none", width = 50, keep_column = FALSE ) %>%
        cols_width(allele_value ~ px(250)) %>%
    cols_label(
      allele_value = md(paste0("*Allele*")),
      Overall="Overall",
      text_Het = md("**Heterozygous**"),
      text_Hom = md("**Homozygous**")) %>%
    # Add style
    gt::tab_style(
      style = list(
        gt::cell_fill(color = "black"), gt::cell_text(color = "white"),
        gt::cell_borders(color = "black", sides = "all")
      ),
      locations = gt::cells_column_labels()
    ) 
}

my_gt_table_simple <- function(data){
  data %>%
    gt() %>%
    # Add barchart
    # gt_plt_bar( column = Overall, color = "black",
    #             scale_type = "none", width = 50, keep_column = FALSE ) %>%
        cols_width(allele_value ~ px(250)) %>%
    cols_label(
      allele_value = md(paste0("*Allele*")),
      Overall="Overall",
      text_Het = md("**Heterozygous**"),
      text_Hom = md("**Homozygous**")) %>%
    # Add style
    gt::tab_style(
      style = list(
        gt::cell_fill(color = "black"), gt::cell_text(color = "white"),
        gt::cell_borders(color = "black", sides = "all")
      ),
      locations = gt::cells_column_labels()
    ) 
}

Normal table

my_gt_table_simple(dummy_counts)

Normal table WITH gt_plt_bar()

my_gt_table(dummy_counts)

Normal table inside a for loop

for (i in 1:3){
  print(i)
  print( my_gt_table_simple(dummy_counts))

}

Normal table WITH gt_plt_bar() inside a for loop

for (i in 1:3){
  print(i)
  print( my_gt_table(dummy_counts))

}

Inside a for loop WITH gt_plt_bar()(without print())

for (i in 1:3){
  print(i)
  my_gt_table(dummy_counts)

}

Expected result

Tables should be able to be printed inside a loop without disrupting the HTML knitting.

Session info

sessionInfo() R version 4.2.3 (2023-03-15) Platform: aarch64-apple-darwin20 (64-bit) Running under: macOS Ventura 13.1

Matrix products: default LAPACK: /Library/Frameworks/R.framework/Versions/4.2-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 base

loaded via a namespace (and not attached): [1] rstudioapi_0.14 xml2_1.3.3 knitr_1.42 magrittr_2.0.3 gt_0.9.0
[6] tidyselect_1.2.0 munsell_0.5.0 colorspace_2.1-0 R6_2.5.1 rlang_1.1.0
[11] fastmap_1.1.1 fansi_1.0.4 dplyr_1.1.1 tools_4.2.3 DT_0.27
[16] grid_4.2.3 trtemplate_1.0.0 gtable_0.3.3 xfun_0.38 utf8_1.2.3
[21] cli_3.6.1 htmltools_0.5.5 yaml_2.3.7 digest_0.6.31 tibble_3.2.1
[26] lifecycle_1.0.3 ggplot2_3.4.2 htmlwidgets_1.6.2 vctrs_0.6.1 glue_1.6.2
[31] evaluate_0.20 rmarkdown_2.21 pillar_1.9.0 compiler_4.2.3 generics_0.1.3
[36] scales_1.2.1 pkgconfig_2.0.3

ait5 commented 1 year ago

Hi! Actually, I was able to work it out with another function: gt_plt_bar_pct()!!

jthomasmock commented 1 year ago

Thanks for the followup @ait5 - closing for now as you've resolved.