gt icon indicating copy to clipboard operation
gt copied to clipboard

Problem with gt::gt_save() in a Ubuntu virtual machine on Azure

Open henrique1008 opened this issue 2 years ago • 9 comments

I try the following code

The usecase is run the code on virtual machine in Azure, Ubuntu 22.04.

table <- mtcars |> 
  gt::gt()
gt::gtsave(table,"/home/ubuntu/test.png")

But I recieved this error and the .png file don't saved

file:////tmp/RtmpiG0iBe/file6444a7dec4bc0.html screenshot completed
Warning messages:
1: In onRejected(...) :
  An error occurred: Error in onFulfilled(value): Unable to find any visible elements for selectors.

2: In normalizePath(value) :
  path[1]="An error occurred: Error in onFulfilled(value): Unable to find any visible elements for selectors.
": No such file or directory

In 0.6.0 version this don't happened, and it's happen and started by exchanging the use of webshot for webshot2 At first I received the error message due to the lack of chrome or chromium But after installing, I received this other

Adding information If I just use webshot2 it works normally

webshot2::webshot("https://www.r-project.org",
                                  file = "test.png")

This code work's perfectly

henrique1008 avatar Aug 30 '22 12:08 henrique1008

I was rendering reports to MS Word. Using gt version 0.6.0, I could not create tables in Word so I used gtsave() to save an image and import into the Rmarkdown for rendering to Word. After updating to gt version 0.7.0, the markdown document would render the png image in my directory, but it would not import into Rmarkdown and the image was not rerndered in the MS Word document. I have removed version 0.7.0 and reverted to version 0.6.0 and all is working as before.

rccline avatar Sep 09 '22 15:09 rccline

Just wanted to contribute that my experience this morning confirms this behavior is apparently unrelated to Azure or VM's. My org models Posit's Shared Baseline env arch, we advanced the date and this bug appeared in the version now available to our environment, taking down a cascade of scheduled reports.

That's on Ubuntu 20 LTS bare metal.

rdewald avatar Dec 05 '22 14:12 rdewald

Hello This problem still persists here for me, I don't know what to do with it and I haven't found a way to solve it webshot2 works normally on my machine, but I can't use gt > 0.6.0 because it uses webshot2

henrique1008 avatar Jun 15 '23 15:06 henrique1008

My problem is in the webshot2 selector parameter

When selector = 'table', it doesn't work, I don't know why yet, I'm also discussing an issue on webshot2's github

https://github.com/rstudio/webshot2/issues/47

henrique1008 avatar Jun 15 '23 16:06 henrique1008

Hi @henrique1008, can you re-run your example and then call sessioninfo::session_info() in the test environment? Then copy and paste the results of that function into a code block here. (The reprex package can also do this for you.) That would help us recreate your environment to investigate.

gadenbuie avatar Jun 15 '23 18:06 gadenbuie

Hi @gadenbuie Thank you for your help

Looking inside the gt::gtsave() function, to save to png it uses a non-exported function called gt:::gt_save_webshot()

The code of gt:::gt_save_webshot() is below:

gt_save_webshot <- function(data, filename, path = NULL, ..., selector = "table",
                            zoom = 2, expand = 5) {
  filename <- gtsave_filename(path = path, filename = filename)
  tempfile_ <- tempfile(fileext = ".html")
  tempfile_ <- tidy_gsub(tempfile_, "\\\\", "/")
  gt_save_html(data = data, filename = tempfile_, path = NULL)
  if (!requireNamespace("webshot2", quietly = TRUE)) {
    cli::cli_abort(c("The `webshot2` package is required for saving images of gt tables.",
      `*` = "It can be installed with `install.packages(\"webshot2\")`."
    ))
  } else {
    webshot::webshot(
      url = paste0("file:///", tempfile_),
      file = filename, selector = selector, zoom = zoom,
      expand = expand, ...
    )
  }
}

I'll reproduce the behavior of the function here

example_table <- mtcars |> gt::gt()
data <- example_table
filename <- "my_table.png"
path = NULL
selector = "table"
zoom = 2
expand = 5

filename <- gt:::gtsave_filename(path = path, filename = filename)
tempfile_ <- tempfile(fileext = ".html")
tempfile_ <- gt:::tidy_gsub(tempfile_, "\\\\", "/")
gt:::gt_save_html(data = data, filename = tempfile_, path = NULL)


webshot2::webshot(
  url = paste0("file:///", tempfile_),
  file = filename,
  selector = selector, 
  zoom = zoom,
  expand = expand, 
)

And I get this error

Registered S3 method overwritten by 'webshot2':
  method        from   
  print.webshot webshot
file:////tmp/RtmplhHera/file10e8dc1471f071.html screenshot completed
Warning messages:
1: In onRejected(...) :
  An error occurred: Error in onFulfilled(value): Unable to find any visible elements for selectors.

2: In normalizePath(value) :
  path[1]="An error occurred: Error in onFulfilled(value): Unable to find any visible elements for selectors.
": No such file or directory

But if I try without selector parameter in webshot2, I don´t get this error, however the .png that is saved is blank

To work around I made a fork of the gt package, and replaced webshot2 with webshot, now I'm able to use the latest version of gt

Session Info

R version 4.2.2 Patched (2022-11-10 r83330)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.5 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0

locale:
 [1] LC_CTYPE=C.UTF-8       LC_NUMERIC=C           LC_TIME=C.UTF-8        LC_COLLATE=C.UTF-8    
 [5] LC_MONETARY=C.UTF-8    LC_MESSAGES=C.UTF-8    LC_PAPER=C.UTF-8       LC_NAME=C             
 [9] LC_ADDRESS=C           LC_TELEPHONE=C         LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C   

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

other attached packages:
[1] gt_0.9.0

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.9       rstudioapi_0.14  webshot2_0.1.0   xml2_1.3.3       magrittr_2.0.3   tidyselect_1.2.0
 [7] R6_2.5.1         rlang_1.1.1      fastmap_1.1.0    fansi_1.0.3      dplyr_1.1.2      tools_4.2.2     
[13] websocket_1.4.1  webshot_0.5.4    utf8_1.2.2       cli_3.6.1        withr_2.5.0      htmltools_0.5.4 
[19] digest_0.6.31    tibble_3.2.1     lifecycle_1.0.3  processx_3.8.0   later_1.3.0      callr_3.7.3     
[25] promises_1.2.0.1 ps_1.7.2         sass_0.4.6       vctrs_0.6.3      fs_1.6.2         glue_1.6.2      
[31] compiler_4.2.2   pillar_1.9.0     generics_0.1.3   chromote_0.1.1   jsonlite_1.8.4   pkgconfig_2.0.3 

henrique1008 avatar Jun 16 '23 12:06 henrique1008

I am also having this issue, on Ubuntu 22. session info attached for what it's worth.

> sessioninfo::session_info()
─ Session info ─────────────────────────────────────────────────────────
 version  R version 4.1.2 (2021-11-01)
 os       Ubuntu 22.04.3 LTS
 system   x86_64, linux-gnu
 ui       RStudio
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       America/Los_Angeles
 date     2023-09-02
 rstudio  2023.06.1+524 Mountain Hydrangea (desktop)
 pandoc   NA

Packages ─────────────────────────────────────────────────────────
 package     * version date (UTC) lib source
 chromote      0.1.2   2023-08-11 [1] CRAN (R 4.1.2)
 cli           3.6.1   2023-03-23 [1] CRAN (R 4.1.2)
 commonmark    1.9.0   2023-03-17 [1] CRAN (R 4.1.2)
 digest        0.6.33  2023-07-07 [1] CRAN (R 4.1.2)
 dplyr         1.1.2   2023-04-20 [1] CRAN (R 4.1.2)
 fansi         1.0.4   2023-01-22 [1] CRAN (R 4.1.2)
 fastmap       1.1.1   2023-02-24 [1] CRAN (R 4.1.2)
 fs            1.6.3   2023-07-20 [1] CRAN (R 4.1.2)
 generics      0.1.3   2022-07-05 [1] CRAN (R 4.1.2)
 glue          1.6.2   2022-02-24 [1] CRAN (R 4.1.2)
 gt          * 0.9.0   2023-03-31 [1] CRAN (R 4.1.2)
 htmltools     0.5.6   2023-08-10 [1] CRAN (R 4.1.2)
 jsonlite      1.8.7   2023-06-29 [1] CRAN (R 4.1.2)
 later         1.3.1   2023-05-02 [1] CRAN (R 4.1.2)
 lifecycle     1.0.3   2022-10-07 [1] CRAN (R 4.1.2)
 magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.1.2)
 markdown      1.8     2023-08-23 [1] CRAN (R 4.1.2)
 pillar        1.9.0   2023-03-22 [1] CRAN (R 4.1.2)
 pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.1.2)
 processx      3.8.2   2023-06-30 [1] CRAN (R 4.1.2)
 promises      1.2.1   2023-08-10 [1] CRAN (R 4.1.2)
 ps            1.7.5   2023-04-18 [1] CRAN (R 4.1.2)
 R6            2.5.1   2021-08-19 [1] CRAN (R 4.1.2)
 Rcpp          1.0.11  2023-07-06 [1] CRAN (R 4.1.2)
 rlang         1.1.1   2023-04-28 [1] CRAN (R 4.1.2)
 rstudioapi    0.15.0  2023-07-07 [1] CRAN (R 4.1.2)
 sass          0.4.7   2023-07-15 [1] CRAN (R 4.1.2)
 sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.1.2)
 tibble        3.2.1   2023-03-20 [1] CRAN (R 4.1.2)
 tidyselect    1.2.0   2022-10-10 [1] CRAN (R 4.1.2)
 utf8          1.2.3   2023-01-31 [1] CRAN (R 4.1.2)
 vctrs         0.6.3   2023-06-14 [1] CRAN (R 4.1.2)
 webshot2      0.1.1   2023-08-11 [1] CRAN (R 4.1.2)
 websocket     1.4.1   2021-08-18 [1] CRAN (R 4.1.2)
 withr         2.5.0   2022-03-03 [1] CRAN (R 4.1.2)
 xfun          0.40    2023-08-09 [1] CRAN (R 4.1.2)
 xml2          1.3.5   2023-07-06 [1] CRAN (R 4.1.2)

sagesteppe avatar Sep 03 '23 03:09 sagesteppe

Another workaround (similar to https://github.com/rstudio/gt/issues/97#issuecomment-469751769). Produce html output and then run webshot manually.

library(gt)
library(webshot)

table <- mtcars |> 
  gt::gt()
gt::gtsave(table,"table.html")

webshot::webshot(url = "table.html", file = "table.png")

AroneyS avatar Oct 25 '23 03:10 AroneyS

Another workaround (similar to #97 (comment)). Produce html output and then run webshot manually.

library(gt)
library(webshot)

table <- mtcars |> 
  gt::gt()
gt::gtsave(table,"table.html")

webshot::webshot(url = "table.html", file = "table.png")

This didn't work for me. Well, it does, but now I get a huge screenshot with a ton of whitespace, which is not ideal.

colebaril avatar Jul 09 '24 02:07 colebaril