Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prefer webshot2 to webshot and solve #1858 #1918

Merged
merged 6 commits into from
Jan 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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