jthomasmock / gtExtras

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

Exporting PNG : `gt_theme_nytimes` title font problem #49

Closed Mathiaslauber closed 2 years ago

Mathiaslauber commented 2 years ago

Hello,

I have created a table :

eumoran <- globalmoraneu %>%
  gt() %>% 
  gt_theme_nytimes() %>% 
  gt_highlight_rows(
    rows = 1:4, 
    fill = "lightgrey",
    bold_target_only = TRUE,
    target_col = Pilier
  ) %>% 
  gt_highlight_rows(
    rows = 5, 
    fill = "#00C189",
    bold_target_only = TRUE,
    target_col = Pilier
  ) %>% 
  gt_highlight_rows(
    rows = 8, 
    fill = "#FF7233",
    bold_target_only = TRUE,
    target_col = Pilier
  ) %>% 

  gt_highlight_rows(
  rows = 12, 
  fill = "#3349FF",
  bold_target_only = TRUE,
  target_col = Pilier
  ) %>%
  tab_header(title = "Autocorrélation spatiale : I de Moran", subtitle = "n : 2288 entreprises [EU]" ) %>% 
  gt_plt_dot(I.Moran, Pilier, palette = "#18343f") %>%
  cols_width(Pilier ~ px(150))
gtsave_extra(eumoran, "IdeMoranEU11.png", path = path, zoom = 1, expand = 5)

Problem is : exported PNG has not the right Title font, but a standard one instead. I have downloaded webshot2, and I used gtsave_extra. Here are screenshot from R and 2nd one is the png file exported. image image

I have found, with the guardian gt theme, that the problem is not appearing.

jthomasmock commented 2 years ago

Howdy @Mathiaslauber !

I converted your question into a reprex and wasn't able to reproduce.

Can you confirm, your gtExtras and webshot2 pkg version?

packageVersion("gtExtras")
packageVersion("webshot2")
library(gtExtras)
library(gt)

mtcars %>%
  head() %>%
  gt() %>% 
  tab_header(
    title = "Autocorrélation spatiale : I de Moran",
    subtitle = "n : 2288 entreprises [EU]" ) %>% 
  gt_theme_nytimes() %>% 
  gtsave_extra("test-tab.png", selector = "table")    
#> If all HTML is not captured, you can try using the 'webshot2' package directly.

knitr::include_graphics("test-tab.png", dpi = 300)

Created on 2022-05-12 by the reprex package (v2.0.1)

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.2.0 (2022-04-22) #> os macOS Monterey 12.2.1 #> system aarch64, darwin20 #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz America/Chicago #> date 2022-05-12 #> pandoc 2.18 @ /Applications/RStudio.app/Contents/MacOS/quarto/bin/tools/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.2.0) #> backports 1.4.1 2021-12-13 [1] CRAN (R 4.2.0) #> callr 3.7.0 2021-04-20 [1] CRAN (R 4.2.0) #> checkmate 2.1.0 2022-04-21 [1] CRAN (R 4.2.0) #> cli 3.3.0 2022-04-25 [1] CRAN (R 4.2.0) #> colorspace 2.0-3 2022-02-21 [1] CRAN (R 4.2.0) #> crayon 1.5.1 2022-03-26 [1] CRAN (R 4.2.0) #> curl 4.3.2 2021-06-23 [1] CRAN (R 4.2.0) #> DBI 1.1.2 2021-12-20 [1] CRAN (R 4.2.0) #> digest 0.6.29 2021-12-01 [1] CRAN (R 4.2.0) #> dplyr 1.0.8 2022-02-08 [1] CRAN (R 4.2.0) #> ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.2.0) #> evaluate 0.15 2022-02-18 [1] CRAN (R 4.2.0) #> fansi 1.0.3 2022-03-24 [1] CRAN (R 4.2.0) #> fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.2.0) #> fontawesome 0.2.2 2021-07-02 [1] CRAN (R 4.2.0) #> fs 1.5.2 2021-12-08 [1] CRAN (R 4.2.0) #> generics 0.1.2 2022-01-31 [1] CRAN (R 4.2.0) #> ggplot2 3.3.5 2021-06-25 [1] CRAN (R 4.2.0) #> glue 1.6.2 2022-02-24 [1] CRAN (R 4.2.0) #> gt * 0.5.0.9000 2022-04-27 [1] Github (rstudio/gt@0d4c83d) #> gtable 0.3.0 2019-03-25 [1] CRAN (R 4.2.0) #> gtExtras * 0.3 2022-04-26 [1] Github (jthomasmock/gtExtras@de9a68f) #> highr 0.9 2021-04-16 [1] CRAN (R 4.2.0) #> htmltools 0.5.2 2021-08-25 [1] CRAN (R 4.2.0) #> httr 1.4.2 2020-07-20 [1] CRAN (R 4.2.0) #> jsonlite 1.8.0 2022-02-22 [1] CRAN (R 4.2.0) #> knitr 1.38.3 2022-04-26 [1] Github (yihui/knitr@87e5d8e) #> lifecycle 1.0.1 2021-09-24 [1] CRAN (R 4.2.0) #> magrittr 2.0.2 2022-01-26 [1] CRAN (R 4.2.0) #> mime 0.12 2021-09-28 [1] CRAN (R 4.2.0) #> munsell 0.5.0 2018-06-12 [1] CRAN (R 4.2.0) #> paletteer 1.4.0 2021-07-20 [1] CRAN (R 4.2.0) #> pillar 1.7.0 2022-02-01 [1] CRAN (R 4.2.0) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.2.0) #> png 0.1-7 2013-12-03 [1] CRAN (R 4.2.0) #> processx 3.5.3 2022-03-25 [1] CRAN (R 4.2.0) #> ps 1.7.0 2022-04-23 [1] CRAN (R 4.2.0) #> purrr 0.3.4 2020-04-17 [1] CRAN (R 4.2.0) #> R.cache 0.15.0 2021-04-30 [1] CRAN (R 4.2.0) #> R.methodsS3 1.8.1 2020-08-26 [1] CRAN (R 4.2.0) #> R.oo 1.24.0 2020-08-26 [1] CRAN (R 4.2.0) #> R.utils 2.11.0 2021-09-26 [1] CRAN (R 4.2.0) #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.2.0) #> rematch2 2.1.2 2020-05-01 [1] CRAN (R 4.2.0) #> reprex 2.0.1 2021-08-05 [1] CRAN (R 4.2.0) #> rlang 1.0.2 2022-03-04 [1] CRAN (R 4.2.0) #> rmarkdown 2.14 2022-04-25 [1] CRAN (R 4.2.0) #> rstudioapi 0.13 2020-11-12 [1] CRAN (R 4.2.0) #> sass 0.4.1.9000 2022-04-26 [1] Github (rstudio/sass@72e722a) #> scales 1.2.0 2022-04-13 [1] CRAN (R 4.2.0) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.2.0) #> stringi 1.7.6 2021-11-29 [1] CRAN (R 4.2.0) #> stringr 1.4.0 2019-02-10 [1] CRAN (R 4.2.0) #> styler 1.7.0 2022-03-13 [1] CRAN (R 4.2.0) #> tibble 3.1.6 2021-11-07 [1] CRAN (R 4.2.0) #> tidyselect 1.1.2 2022-02-21 [1] CRAN (R 4.2.0) #> utf8 1.2.2 2021-07-24 [1] CRAN (R 4.2.0) #> vctrs 0.4.1 2022-04-13 [1] CRAN (R 4.2.0) #> webshot 0.5.3 2022-04-14 [1] CRAN (R 4.2.0) #> withr 2.5.0 2022-03-03 [1] CRAN (R 4.2.0) #> xfun 0.30 2022-03-02 [1] CRAN (R 4.2.0) #> xml2 1.3.3 2021-11-30 [1] CRAN (R 4.2.0) #> yaml 2.3.5 2022-02-21 [1] CRAN (R 4.2.0) #> #> [1] /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library #> #> ────────────────────────────────────────────────────────────────────────────── ```
jthomasmock commented 2 years ago

Howdy @Mathiaslauber - closing this for now, let me know if you aren't able to confirm the reprex as seen above.

Mathiaslauber commented 2 years ago

@jthomasmock Thank you for your answer, sorry I'm answering late. Busy writing thesis. Your code actually work, but i have same problem exporting png : headers with wrong font.

I tried 2 things (version A, B): Basically exporting png before and after creating table. Note : the table font is correct in Rstudio preview. In both case after exporting, header font is arial.

Registered S3 method overwritten by 'webshot':
  method        from    
  print.webshot webshot2
If all HTML is not captured, you can try using the 'webshot2' package directly.
library("webshot2")
library("gtExtras")
library("gt")

version a
plot <- mtcars %>%
  head() %>%
  gt() %>% 
  tab_header(
    title = "Autocorrélation spatialee : I de Moran",
    subtitle = "n : 2288 entreprises [EU]" ) %>% 
  gt_theme_nytimes()  %>% 
  gtsave_extra("E:/Dropbox/Travail master/0_data/R/extraction/13_moran_tables/Output/test-tab.png", selector = "table")  

version b
plot <- mtcars %>%
  head() %>%
  gt() %>% 
  tab_header(
    title = "Autocorrélation spatialee : I de Moran",
    subtitle = "n : 2288 entreprises [EU]" ) %>% 
  gt_theme_nytimes() 

path <- "13_moran_tables/Output/"
gtsave(plot, "try1.png", path = path)
gtsave_extra(plot, "try2.png", path = path)

Here version on packages as requested :

packageVersion("gtExtras") [1] ‘0.3.1’ packageVersion("webshot2") [1] ‘0.0.0.9000’

Session Info Session info below ``` Session info ------------------------------------------------------------------------------------------------------------ setting value version R version 4.1.3 (2022-03-10) os Windows 10 x64 (build 19044) system x86_64, mingw32 ui RStudio language (EN) collate French_Switzerland.1252 ctype French_Switzerland.1252 tz Europe/Berlin date 2022-05-24 rstudio 2022.02.1+461 Prairie Trillium (desktop) pandoc 2.17.1.1 @ C:/Program Files/RStudio/bin/quarto/bin/ (via rmarkdown) - Packages ---------------------------------------------------------------------------------------------------------------- package * version date (UTC) lib source abind 1.4-5 2016-07-21 [1] CRAN (R 4.1.1) assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.1.3) backports 1.4.1 2021-12-13 [1] CRAN (R 4.1.2) bbplot * 0.2 2022-05-02 [1] Github (bbc/bbplot@82af595) brio 1.1.3 2021-11-30 [1] CRAN (R 4.1.3) broom 0.7.12 2022-01-28 [1] CRAN (R 4.1.3) cachem 1.0.6 2021-08-19 [1] CRAN (R 4.1.3) callr 3.7.0 2021-04-20 [1] CRAN (R 4.1.3) car 3.0-12 2021-11-06 [1] CRAN (R 4.1.3) carData 3.0-5 2022-01-06 [1] CRAN (R 4.1.3) cellranger 1.1.0 2016-07-27 [1] CRAN (R 4.1.3) checkmate 2.0.0 2020-02-06 [1] CRAN (R 4.1.3) chromote 0.1.0 2022-05-09 [1] Github (rstudio/chromote@edaefdf) cli 3.2.0 2022-02-14 [1] CRAN (R 4.1.3) colorspace 2.0-3 2022-02-21 [1] CRAN (R 4.1.3) crayon 1.5.1 2022-03-26 [1] CRAN (R 4.1.3) data.table * 1.14.2 2021-09-27 [1] CRAN (R 4.1.3) DBI 1.1.2 2021-12-20 [1] CRAN (R 4.1.3) dbplyr 2.1.1 2021-04-06 [1] CRAN (R 4.1.3) desc 1.4.1 2022-03-06 [1] CRAN (R 4.1.3) devtools 2.4.3 2021-11-30 [1] CRAN (R 4.1.3) digest 0.6.29 2021-12-01 [1] CRAN (R 4.1.3) dplyr * 1.0.8 2022-02-08 [1] CRAN (R 4.1.3) ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.1.3) evaluate 0.15 2022-02-18 [1] CRAN (R 4.1.3) extrafont * 0.18 2022-04-12 [1] CRAN (R 4.1.3) extrafontdb 1.0 2012-06-11 [1] CRAN (R 4.1.1) fansi 1.0.3 2022-03-24 [1] CRAN (R 4.1.3) farver 2.1.0 2021-02-28 [1] CRAN (R 4.1.3) fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.1.3) fontawesome 0.2.2 2021-07-02 [1] CRAN (R 4.1.3) forcats * 0.5.1 2021-01-27 [1] CRAN (R 4.1.3) formattable * 0.2.1 2021-01-07 [1] CRAN (R 4.1.3) fs 1.5.2 2021-12-08 [1] CRAN (R 4.1.3) generics 0.1.2 2022-01-31 [1] CRAN (R 4.1.3) ggplot2 * 3.3.5 2021-06-25 [1] CRAN (R 4.1.3) ggpubr * 0.4.0 2020-06-27 [1] CRAN (R 4.1.3) ggsignif 0.6.3 2021-09-09 [1] CRAN (R 4.1.3) ggthemes * 4.2.4 2021-01-20 [1] CRAN (R 4.1.3) glue 1.6.2 2022-02-24 [1] CRAN (R 4.1.3) gt * 0.6.0 2022-05-24 [1] CRAN (R 4.1.3) gtable 0.3.0 2019-03-25 [1] CRAN (R 4.1.3) gtExtras * 0.3.1 2022-05-09 [1] Github (jthomasmock/gtExtras@25269ed) haven 2.4.3 2021-08-04 [1] CRAN (R 4.1.3) hms 1.1.1 2021-09-26 [1] CRAN (R 4.1.3) htmltools 0.5.2 2021-08-25 [1] CRAN (R 4.1.3) htmlwidgets 1.5.4 2021-09-08 [1] CRAN (R 4.1.3) httr 1.4.2 2020-07-20 [1] CRAN (R 4.1.3) janitor 2.1.0 2021-01-05 [1] CRAN (R 4.1.3) jsonlite 1.8.0 2022-02-22 [1] CRAN (R 4.1.3) knitr * 1.39 2022-04-26 [1] CRAN (R 4.1.3) later 1.3.0 2021-08-18 [1] CRAN (R 4.1.3) lifecycle 1.0.1 2021-09-24 [1] CRAN (R 4.1.3) lubridate 1.8.0 2021-10-07 [1] CRAN (R 4.1.3) magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.1.3) memoise 2.0.1 2021-11-26 [1] CRAN (R 4.1.3) modelr 0.1.8 2020-05-19 [1] CRAN (R 4.1.3) munsell 0.5.0 2018-06-12 [1] CRAN (R 4.1.3) paletteer 1.4.0 2021-07-20 [1] CRAN (R 4.1.3) pillar 1.7.0 2022-02-01 [1] CRAN (R 4.1.3) pkgbuild 1.3.1 2021-12-20 [1] CRAN (R 4.1.3) pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.1.3) pkgload 1.2.4 2021-11-30 [1] CRAN (R 4.1.3) prettyunits 1.1.1 2020-01-24 [1] CRAN (R 4.1.3) processx 3.5.3 2022-03-25 [1] CRAN (R 4.1.3) promises 1.2.0.1 2021-02-11 [1] CRAN (R 4.1.3) ps 1.6.0 2021-02-28 [1] CRAN (R 4.1.3) purrr * 0.3.4 2020-04-17 [1] CRAN (R 4.1.3) R6 2.5.1 2021-08-19 [1] CRAN (R 4.1.3) Rcpp 1.0.8.3 2022-03-17 [1] CRAN (R 4.1.3) readr * 2.1.2 2022-01-30 [1] CRAN (R 4.1.3) readxl * 1.4.0 2022-03-28 [1] CRAN (R 4.1.3) rematch2 2.1.2 2020-05-01 [1] CRAN (R 4.1.3) remotes 2.4.2 2021-11-30 [1] CRAN (R 4.1.3) reprex 2.0.1 2021-08-05 [1] CRAN (R 4.1.3) rlang 1.0.2 2022-03-04 [1] CRAN (R 4.1.3) rmarkdown 2.13 2022-03-10 [1] CRAN (R 4.1.3) rprojroot 2.0.3 2022-04-02 [1] CRAN (R 4.1.3) rstatix 0.7.0 2021-02-13 [1] CRAN (R 4.1.3) rstudioapi 0.13 2020-11-12 [1] CRAN (R 4.1.3) Rttf2pt1 1.3.10 2022-02-07 [1] CRAN (R 4.1.2) rvest 1.0.2 2021-10-16 [1] CRAN (R 4.1.3) sass 0.4.1 2022-03-23 [1] CRAN (R 4.1.3) scales 1.1.1 2020-05-11 [1] CRAN (R 4.1.3) sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.1.3) showtext * 0.9-5 2022-02-09 [1] CRAN (R 4.1.3) showtextdb * 3.0 2020-06-04 [1] CRAN (R 4.1.3) snakecase 0.11.0 2019-05-25 [1] CRAN (R 4.1.3) stringi 1.7.6 2021-11-29 [1] CRAN (R 4.1.2) stringr * 1.4.0 2019-02-10 [1] CRAN (R 4.1.3) sysfonts * 0.8.8 2022-03-13 [1] CRAN (R 4.1.3) testthat 3.1.3 2022-03-29 [1] CRAN (R 4.1.3) tibble * 3.1.6 2021-11-07 [1] CRAN (R 4.1.3) tidyr * 1.2.0 2022-02-01 [1] CRAN (R 4.1.3) tidyselect 1.1.2 2022-02-21 [1] CRAN (R 4.1.3) tidyverse * 1.3.1 2021-04-15 [1] CRAN (R 4.1.3) tzdb 0.3.0 2022-03-28 [1] CRAN (R 4.1.3) usethis 2.1.5 2021-12-09 [1] CRAN (R 4.1.3) utf8 1.2.2 2021-07-24 [1] CRAN (R 4.1.3) vctrs 0.4.0 2022-03-30 [1] CRAN (R 4.1.3) webshot * 0.5.3 2022-04-14 [1] CRAN (R 4.1.3) webshot2 * 0.0.0.9000 2022-05-09 [1] Github (rstudio/webshot2@6cc8950) websocket 1.4.1 2021-08-18 [1] CRAN (R 4.1.3) withr 2.5.0 2022-03-03 [1] CRAN (R 4.1.3) xfun 0.30 2022-03-02 [1] CRAN (R 4.1.3) xml2 1.3.3 2021-11-30 [1] CRAN (R 4.1.3) [1] C:/Users/AXMat/Documents/R/win-library/4.1 [2] C:/Program Files/R/R-4.1.3/library ```
jthomasmock commented 2 years ago

Gotcha, so when that message prints: use webshot2 directly it means to call something like:

webshot2::webshot("file.html", "output.png")

You're still using webshot::webshot() in the calls as reported with the older version of gtExtras::gtsave_extra().

Now that webshot2 is on CRAN, I've updated the internals to use webshot2 for everything.

Can you try:

Based off the session info I see you're on Windows, and it's possible there are some path issues (an actual bug) but my initial suspicion is that it's a limitation of the older webshot vs webshot2.

Mathiaslauber commented 2 years ago

@jthomasmock

jthomasmock commented 2 years ago

Woohoo!