Problems when saving a {gt} table

Hi together,

I use the {gt} package for the first time and really enjoy it. While the table looked as epxected and complete in the Rstudio viewer pane and when run locally in any browser, I spend now most of the time figuring out how to save the table so it (a) contains the right fonts and (b) contains all ggplots and image files. And I failed so far.

Unfortunately, the code is too massive to upload it as a reprex but you can find the code on GitHub.

(Note: I keep this list updated every time a new suggestion comes in.)

What I tried:

  • gtsave(gt, "gt.html")
  • gtsave(gt, "gt.html", inline_css = TRUE)
  • gtsave(gt, "gt.png")
  • gtsave(gt, "gt.png", expand = 20)
  • gtsave(gt, "gt.png", expand = 20, delay = 1000)
  • knit Rmd to html
  • knit Rmd to html via gt() %>% as_raw_html(inline_css = TRUE)
  • export via htmlWidgets::saveWidget(gt, "gt.html") (gt objects seems not to be supported)

What happened:

  • The html files contains the correct fonts when saved with inline_css = TRUE but are missing the second ggplot column (I have two columns with visualizations), the one that contains way more complex and bigger plots. It is also missing the film posters (images via url) in the second column.

  • The png files contains the images and almost all plots. Almost? Yes, from a certain row onwards the plots simply disappear (e.g. from row 83 onwards). Also, the emoji symbol is not rendered as expected. The delay seemed as a perfect candidate to solve it but it didn't change anything (same number of plots that are included/not included).

  • The html inside the markdown report neither contains plots or images nor the correct fonts (fine in case of Overpass Mono and Bitter but not with the main font family Roboto Condensed - all of them are installed on my system and work locally, in the html and png). Also the font is bigger than specified in some of these columns and aspect ratios are completely ignored for plots and images. Emoji symbols are missing completely. When using the as_raw_html() the simple ggplots are all included but none of the web images nor any of the more complex ggplots. Again, some fonts are fine but some are not. Emojis are all missing.

Does anyone have an idea what's going on? Are the ggplot's simply too big and ambitious? Is there any other way to save it to html?

I am grateful for any hints and tipps!
Cheers, Cédric

> sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)

Matrix products: default

locale:
[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252    LC_MONETARY=German_Germany.1252 LC_NUMERIC=C                   
[5] LC_TIME=German_Germany.1252    
system code page: 65001

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

other attached packages:
 [1] ragg_0.3.1         glue_1.4.1         here_0.1           gt_0.2.2           forcats_0.5.0      stringr_1.4.0     
 [7] dplyr_1.0.1        purrr_0.3.4        readr_1.3.1        tidyr_1.1.1        tibble_3.0.3       ggplot2_3.3.2.9000
[13] tidyverse_1.3.0   

loaded via a namespace (and not attached):
 [1] tidyselect_1.1.0  xfun_0.16         haven_2.3.1       colorspace_1.4-1  vctrs_0.3.2       generics_0.0.2    htmltools_0.5.0  
 [8] yaml_2.2.1        blob_1.2.1        rlang_0.4.7       pillar_1.4.6      withr_2.2.0       DBI_1.1.0         dbplyr_1.4.4     
[15] modelr_0.1.8      readxl_1.3.1      lifecycle_0.2.0   munsell_0.5.0     gtable_0.3.0      cellranger_1.1.0  rvest_0.3.6      
[22] knitr_1.29        fansi_0.4.1       broom_0.7.0       Rcpp_1.0.5        scales_1.1.1      backports_1.1.7   jsonlite_1.7.0   
[29] systemfonts_0.2.3 fs_1.5.0          hms_0.5.3         packrat_0.5.0     digest_0.6.25     stringi_1.4.6     grid_4.0.2       
[36] rprojroot_1.3-2   cli_2.0.2         tools_4.0.2       magrittr_1.5      crayon_1.3.4      pkgconfig_2.0.3   ellipsis_0.3.1   
[43] xml2_1.3.2        reprex_0.3.0      lubridate_1.7.9   assertthat_0.2.1  httr_1.4.2        rstudioapi_0.11   R6_2.4.1         
[50] compiler_4.0.2  

I would try library(htmlWidgets) saveWidget function

Thank you @nirgrahamuk! Unfortunately, I get the following error message. I've never used saveWidget() before and never seen this type of error. Sounds as the package is not installed properly but it is. Is this error related to my arguments or related to the fact a gt object doesn't work with it?

> saveWidget(gt, "gt.html")
Error in .getNamespace(pkg) : 
  invalid type/length (symbol/0) in vector allocation

Hey Cedric!

Thanks for sharing these details.

Can you try saving it as PDF via gtsave() - and lastly try inside a RMarkdown that you intend to knit:

gt() %>% as_raw_html(inline_css = TRUE)

Also, as you mentioned the ggplot2 images may be overwhelming. Any chance you could use some of the plotting functions from kableExtra that generate more lightweight SVG plots?

Quick example here and here:

I know I've run into problems with lots of ggplot embeds taking a lot of time, since you have to create it, export it, import it, and then embed it and the file size/load-time can balloon as such. It may just be timing out?

1 Like

Hey Thomas, thanks for your suggestions.

Yes, it seems the images are simply too big. This is now kinda confirmed via gtsave("gt.pdf") since it returns Memory exhausted. and then the error Error in webshot::webshot(url = paste0("file:///", tempfile_), file = filename, : webshot.js returned failure value: 1.

Exporting it via as_raw_html() contains one column (the simple ggplots) but not the URL images nor the more complex ggplots. THge emoji is missing as well and not all fonts are correct :confused:

If I get it right I can't use kableExtra::spec_plot() since it is meant to create simple plots but not to turn complex ggplots into light-weight versions? Please correct me if I am wrong.

Gotcha, yah it's a BIG table, so it may be finding some edge-cases on the export options.

spec_plot() is essentially for sparkline-style plots (histogram, line plot, dot plot) or summary stats (boxplot and equivalent). So yes, only very simple plots, and actually done via SVG + base plotting as far as I know.

In theory you may be able to do something similar with ggplot2 but I haven't been able to create a clean example.

Okay, so I systematically tried it with smaller tables. The Top 50 still does not contain all plots in the last column but when goign down to e.g. 10 rows it works. The html however still does only contain one row of ggplots (the runtime column) but not the trend stripes and no poster images. Very strange.

PNG:

HTML:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.