rstudio / webshot2

Take screenshots of web pages from R
https://rstudio.github.io/webshot2/
109 stars 17 forks source link

Pass width and height dimension in percents #48

Open cderv opened 1 year ago

cderv commented 1 year ago

This comes from https://github.com/rstudio/rmarkdown/issues/2460

In knitr we were using webshot but now default to webshot2 is installed on the user side so that we can do screenshots of html widgets to include in PDF for example. Dimensions for the screenshot is passed from value set in chunk options out.width and out.height converted in px units, except if passed in % that we keep. This means for such chunk

```{r}
#| out.height="100%"
library(plotly)
p <- plot_ly(economics, x = ~date, y = ~unemploy / pop)
p

this will be equivalent to passing `vwidth = 468` and `vheight = "100%`. 

Simulating that outside of **knitr** we get the error also

webshot2::webshot("https://github.com/rstudio/shiny", vwidth = 468, vheight = "100%") Error in s$close() : attempt to apply non-function

Full backtrace ```` Error in `s$close()`: ! attempt to apply non-function --- Backtrace: ▆ 1. └─webshot2::webshot(...) 2. └─cm$wait_for(p) 3. └─chromote:::synchronize(p, loop = private$child_loop) 4. ├─promises::with_promise_domain(...) 5. │ └─domain$wrapSync(expr) 6. │ └─base::force(expr) 7. └─base::tryCatch(...) 8. └─base (local) tryCatchList(expr, classes, parentenv, handlers) 9. └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]]) 10. └─base (local) doTryCatch(return(expr), name, parentenv, handler) ````

I don't know if this is a chromote error only, or a webshot2 one

Or maybe v(height|width) can't be set in % - but if this is the case, then it could be checked early and error more friendly. We would also need to decide what to pass as value from knitr.

Precision on why I consider this a potential error in webshot2 - It was working with webshot

res <- webshot::webshot("https://github.com/rstudio/shiny", vwidth = 468, vheight = "100%")
str(res)
#> 'webshot' chr "webshot.png"
cderv commented 6 months ago

This is kind of related (and also reported as part of https://github.com/rstudio/rmarkdown/issues/2460

fig.width = 6; dpi = 72; fig.asp = 0.618
webshot2::webshot("https://github.com/rstudio/shiny", vwidth = fig.width * dpi, vheight = fig.width * dpi * fig.asp)
#> Error in s$close(): tentative d'appliquer un objet qui n'est pas une fonction

It seems vheight does accept only integer really.

If this is requirement, there could be some checks, and better error message. Then we can decide how to process the parameters in knitr to pass them correctly for snapshoting. webshot R package supported those values, so it is appearing just now.