Skip to content

Commit

Permalink
close #1858: prefer webshot2 over webshot if webshot2 and Chrome are …
Browse files Browse the repository at this point in the history
…available (#1918)
  • Loading branch information
atusy authored Jan 29, 2021
1 parent b91452e commit 20f8571
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGES IN knitr VERSION 1.32

## NEW FEATURES

- The internal function `html_screenshot()`, which takes screenshots of HTML widgets and Shiny apps in **knitr** documents, now prefers the **webshot2** package over the **webshot** package. This is because the backend of the **webshot** package is PhantomJS, which [has been archived since 2018](https://github.com/ariya/phantomjs/issues/15344). If **webshot** is still preferred, use the chunk option `opts_chunk$set(webshot = "webshot")` (thanks, @atusy #1918, @LouisStAmour #1858).

## BUG FIXES

- Graphical parameter `par(col =)` is now preserved as expected in following chunks when `knitr::opts_knit$set(global.par = TRUE)` (thanks, @ArnonL, @RolandASc, #1603).
Expand Down
21 changes: 16 additions & 5 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -511,18 +511,27 @@ need_screenshot = function(x, ...) {
if (length(fmt) == 0 || force) return(i1 || i2 || i3)
html_format = fmt %in% c('html', 'html4', 'html5', 'revealjs', 's5', 'slideous', 'slidy')
res = ((i1 || i3) && !html_format) || (i2 && !(html_format && runtime_shiny()))
res && webshot_available()
res && any(webshot_available())
}

runtime_shiny = function() {
identical(opts_knit$get('rmarkdown.runtime'), 'shiny')
}

webshot_available = local({
res = NULL # cache the availablity of webshot/PhantomJS
res = NULL # cache the availability of webshot2/Chrome and webshot/PhantomJS
function() {
if (is.null(res))
res <<- loadable('webshot') && !is.null(getFromNamespace('find_phantom', 'webshot')())
res <<- c(
webshot2 = (
loadable('webshot2') &&
tryCatch(
file.exists(getFromNamespace('find_chrome', 'chromote')()),
error = function(e) FALSE
)
),
webshot = loadable('webshot') && !is.null(getFromNamespace('find_phantom', 'webshot')())
)
res
}
})
Expand Down Expand Up @@ -550,18 +559,20 @@ html_screenshot = function(x, options = opts_current$get(), ...) {
if (is.null(wargs$delay)) wargs$delay = if (i1) 0.2 else 1
d = tempfile()
dir.create(d); on.exit(unlink(d, recursive = TRUE), add = TRUE)
w = webshot_available()
webshot = c(options$webshot, names(w)[w])[[1L]]
f = in_dir(d, {
if (i1 || i3) {
if (i1) {
f1 = wd_tempfile('widget', '.html')
save_widget(x, f1, FALSE, options = options)
} else f1 = x$url
f2 = wd_tempfile('webshot', ext)
f3 = do.call(webshot::webshot, c(list(f1, f2), wargs))
f3 = do.call(getFromNamespace('webshot', webshot), c(list(f1, f2), wargs))
normalizePath(f3)
} else if (i2) {
f1 = wd_tempfile('webshot', ext)
f2 = do.call(webshot::appshot, c(list(x, f1), wargs))
f2 = do.call(getFromNamespace('appshot', webshot), c(list(x, f1), wargs))
normalizePath(f2)
}
})
Expand Down

0 comments on commit 20f8571

Please sign in to comment.