From 095da3d40523eb7fd166e19c1aa8086331689e88 Mon Sep 17 00:00:00 2001 From: Sebastian Gatscha Date: Wed, 28 Dec 2022 10:13:18 +0100 Subject: [PATCH 1/5] allow JPG / JPEG, add verbose argument --- R/webshot.R | 21 ++++++++++++++------- man/webshot.Rd | 5 ++++- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/R/webshot.R b/R/webshot.R index 32f42c0..44cf7cd 100644 --- a/R/webshot.R +++ b/R/webshot.R @@ -53,6 +53,7 @@ NULL #' device (but using zoom will not report that there is a HiDPI device). #' @param useragent The User-Agent header used to request the URL. #' @param max_concurrent (Currently not implemented) +#' @param verbose Show / Hide console messages #' @template webshot-return #' #' @examples @@ -117,7 +118,8 @@ webshot <- function( delay = 0.2, zoom = 1, useragent = NULL, - max_concurrent = getOption("webshot.concurrent", default = 6) + max_concurrent = getOption("webshot.concurrent", default = 6), + verbose = TRUE ) { if (length(url) == 0) { @@ -196,7 +198,8 @@ webshot <- function( function(args) { new_session_screenshot(cm, args$url, args$file, args$vwidth, args$vheight, args$selector, - args$cliprect, args$expand, args$delay, args$zoom, args$useragent + args$cliprect, args$expand, args$delay, args$zoom, args$useragent, + verbose ) } ) @@ -219,12 +222,14 @@ new_session_screenshot <- function( expand, delay, zoom, - useragent + useragent, + verbose ) { filetype <- tolower(tools::file_ext(file)) - if (filetype != "png" && filetype != "pdf") { - stop("File extension must be 'png' or 'pdf'") + filetypes <- c("png","pdf","jpg","jpeg") + if (!filetype %in% filetypes) { + stop("File extension must be one of: ", paste(filetypes, collapse = ", ")) } if (is.null(selector)) { @@ -275,7 +280,7 @@ new_session_screenshot <- function( } })$ then(function(value) { - if (filetype == "png") { + if (filetype == "png" || filetype == "jpg" || filetype == "jpeg") { s$screenshot( filename = file, selector = selector, cliprect = cliprect, expand = expand, scale = zoom, @@ -287,7 +292,9 @@ new_session_screenshot <- function( } })$ then(function(value) { - message(url, " screenshot completed") + if (verbose) { + message(url, " screenshot completed") + } normalizePath(value) })$ finally(function() { diff --git a/man/webshot.Rd b/man/webshot.Rd index 7ec466b..3ad0057 100644 --- a/man/webshot.Rd +++ b/man/webshot.Rd @@ -15,7 +15,8 @@ webshot( delay = 0.2, zoom = 1, useragent = NULL, - max_concurrent = getOption("webshot.concurrent", default = 6) + max_concurrent = getOption("webshot.concurrent", default = 6), + verbose = TRUE ) } \arguments{ @@ -75,6 +76,8 @@ device (but using zoom will not report that there is a HiDPI device).} \item{useragent}{The User-Agent header used to request the URL.} \item{max_concurrent}{(Currently not implemented)} + +\item{verbose}{Show / Hide console messages} } \value{ Invisibly returns the normalized path to all screenshots taken. The From df98e7c0c336d6cd17ec618e274e25ada4460395 Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Wed, 9 Aug 2023 09:45:20 -0400 Subject: [PATCH 2/5] rename: `quiet` instead of `verbose` --- R/webshot.R | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/R/webshot.R b/R/webshot.R index 44cf7cd..bff0bcd 100644 --- a/R/webshot.R +++ b/R/webshot.R @@ -53,7 +53,7 @@ NULL #' device (but using zoom will not report that there is a HiDPI device). #' @param useragent The User-Agent header used to request the URL. #' @param max_concurrent (Currently not implemented) -#' @param verbose Show / Hide console messages +#' @param quiet If `TRUE`, status updates via console messages are suppressed. #' @template webshot-return #' #' @examples @@ -119,7 +119,7 @@ webshot <- function( zoom = 1, useragent = NULL, max_concurrent = getOption("webshot.concurrent", default = 6), - verbose = TRUE + quiet = getOption("webshot.quiet", default = FALSE) ) { if (length(url) == 0) { @@ -199,7 +199,7 @@ webshot <- function( new_session_screenshot(cm, args$url, args$file, args$vwidth, args$vheight, args$selector, args$cliprect, args$expand, args$delay, args$zoom, args$useragent, - verbose + quiet ) } ) @@ -223,7 +223,7 @@ new_session_screenshot <- function( delay, zoom, useragent, - verbose + quiet ) { filetype <- tolower(tools::file_ext(file)) @@ -292,9 +292,7 @@ new_session_screenshot <- function( } })$ then(function(value) { - if (verbose) { - message(url, " screenshot completed") - } + if (!isTRUE(quiet)) message(url, " screenshot completed") normalizePath(value) })$ finally(function() { From c427f0474c3e6983df81f9021c79612b299072dd Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Wed, 9 Aug 2023 09:53:11 -0400 Subject: [PATCH 3/5] feat: Support webp images --- DESCRIPTION | 2 +- R/webshot.R | 10 +++++++--- man/appshot.Rd | 3 ++- man/rmdshot.Rd | 3 ++- man/webshot.Rd | 7 ++++--- 5 files changed, 16 insertions(+), 9 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index b35e563..3dbb2c1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -22,7 +22,7 @@ Suggests: Depends: R (>= 3.2) License: GPL-2 Encoding: UTF-8 -RoxygenNote: 7.2.0 +RoxygenNote: 7.2.3 URL: https://github.com/rstudio/webshot2 BugReports: https://github.com/rstudio/webshot2/issues Language: en-US diff --git a/R/webshot.R b/R/webshot.R index bff0bcd..50e568c 100644 --- a/R/webshot.R +++ b/R/webshot.R @@ -9,7 +9,8 @@ NULL #' #' @param url A vector of URLs to visit. If multiple URLs are provided, it will #' load and take screenshots of those web pages in parallel. -#' @param file A vector of names of output files. Should end with \code{.png} or +#' @param file A vector of names of output files. Should end with an image file +#' type (\code{.png}, \code{.jpg}, \code{.jpeg}, or \code{.webp}) or #' \code{.pdf}. If several screenshots have to be taken and only one filename #' is provided, then the function appends the index number of the screenshot #' to the file name. For PDF output, it is just like printing the page to PDF @@ -227,7 +228,7 @@ new_session_screenshot <- function( ) { filetype <- tolower(tools::file_ext(file)) - filetypes <- c("png","pdf","jpg","jpeg") + filetypes <- c(webshot_image_types(), "pdf") if (!filetype %in% filetypes) { stop("File extension must be one of: ", paste(filetypes, collapse = ", ")) } @@ -280,7 +281,7 @@ new_session_screenshot <- function( } })$ then(function(value) { - if (filetype == "png" || filetype == "jpg" || filetype == "jpeg") { + if (filetype %in% webshot_image_types()) { s$screenshot( filename = file, selector = selector, cliprect = cliprect, expand = expand, scale = zoom, @@ -302,6 +303,9 @@ new_session_screenshot <- function( p } +webshot_image_types <- function() { + c("png", "jpg", "jpeg", "webp") +} knit_print.webshot <- function(x, ...) { lapply(x, function(filename) { diff --git a/man/appshot.Rd b/man/appshot.Rd index 5439550..d76ee68 100644 --- a/man/appshot.Rd +++ b/man/appshot.Rd @@ -34,7 +34,8 @@ appshot( \arguments{ \item{app}{A Shiny app object, or a string naming an app directory.} -\item{file}{A vector of names of output files. Should end with \code{.png} or +\item{file}{A vector of names of output files. Should end with an image file +type (\code{.png}, \code{.jpg}, \code{.jpeg}, or \code{.webp}) or \code{.pdf}. If several screenshots have to be taken and only one filename is provided, then the function appends the index number of the screenshot to the file name. For PDF output, it is just like printing the page to PDF diff --git a/man/rmdshot.Rd b/man/rmdshot.Rd index 8399761..3061fb2 100644 --- a/man/rmdshot.Rd +++ b/man/rmdshot.Rd @@ -17,7 +17,8 @@ rmdshot( \arguments{ \item{doc}{The path to a Rmd document.} -\item{file}{A vector of names of output files. Should end with \code{.png} or +\item{file}{A vector of names of output files. Should end with an image file +type (\code{.png}, \code{.jpg}, \code{.jpeg}, or \code{.webp}) or \code{.pdf}. If several screenshots have to be taken and only one filename is provided, then the function appends the index number of the screenshot to the file name. For PDF output, it is just like printing the page to PDF diff --git a/man/webshot.Rd b/man/webshot.Rd index 3ad0057..36e5663 100644 --- a/man/webshot.Rd +++ b/man/webshot.Rd @@ -16,14 +16,15 @@ webshot( zoom = 1, useragent = NULL, max_concurrent = getOption("webshot.concurrent", default = 6), - verbose = TRUE + quiet = getOption("webshot.quiet", default = FALSE) ) } \arguments{ \item{url}{A vector of URLs to visit. If multiple URLs are provided, it will load and take screenshots of those web pages in parallel.} -\item{file}{A vector of names of output files. Should end with \code{.png} or +\item{file}{A vector of names of output files. Should end with an image file +type (\code{.png}, \code{.jpg}, \code{.jpeg}, or \code{.webp}) or \code{.pdf}. If several screenshots have to be taken and only one filename is provided, then the function appends the index number of the screenshot to the file name. For PDF output, it is just like printing the page to PDF @@ -77,7 +78,7 @@ device (but using zoom will not report that there is a HiDPI device).} \item{max_concurrent}{(Currently not implemented)} -\item{verbose}{Show / Hide console messages} +\item{quiet}{If `TRUE`, status updates via console messages are suppressed.} } \value{ Invisibly returns the normalized path to all screenshots taken. The From 4e7cde82f4e146e9eb1ac6dee936b602aba0e965 Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Wed, 9 Aug 2023 09:56:51 -0400 Subject: [PATCH 4/5] docs: Update NEWS --- NEWS.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/NEWS.md b/NEWS.md index 82cd27e..163eece 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,7 +1,10 @@ # webshot2 (development version) +* `webshot()` now supports JPEG (`.jpg` or `.jpeg`) and WEBP (`.webp`) image formats. (@trafficonese #45) + * Fixed #52: `rmdshot()` did not work when used to screenshot an R Markdown document with `runtime: shiny` or `runtime: shinyrmd`. (#53) +* Fixed #24: Console messages from `webshot()` can now be suppressed by setting `quiet = TRUE` or using the `webshot.quiet` global option. (@trafficonese #45) # webshot2 0.1.0 * Added a `NEWS.md` file to track changes to the package. From 7a9ee754d8cf4fdfddc36b0a2465e46b32b6feef Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Wed, 9 Aug 2023 09:57:16 -0400 Subject: [PATCH 5/5] docs: fix news line spacing --- NEWS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS.md b/NEWS.md index 163eece..c49aaca 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,6 +5,7 @@ * Fixed #52: `rmdshot()` did not work when used to screenshot an R Markdown document with `runtime: shiny` or `runtime: shinyrmd`. (#53) * Fixed #24: Console messages from `webshot()` can now be suppressed by setting `quiet = TRUE` or using the `webshot.quiet` global option. (@trafficonese #45) + # webshot2 0.1.0 * Added a `NEWS.md` file to track changes to the package.