Skip to content

Commit

Permalink
v0.1.0 (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
schloerke authored May 25, 2022
1 parent 4146aa0 commit ce1ffab
Show file tree
Hide file tree
Showing 17 changed files with 190 additions and 58 deletions.
3 changes: 3 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
^_pkgdown\.yml$
^docs$
^pkgdown$
^cran-comments\.md$
^CRAN-SUBMISSION$
^man-roxygen$
3 changes: 3 additions & 0 deletions CRAN-SUBMISSION
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Version: 0.1.0
Date: 2022-05-12 17:03:24 UTC
SHA: 69cbecdbe9d1ea72fcca2aa8af95f79d5fec9bb5
21 changes: 11 additions & 10 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
Package: webshot2
Title: Take Screenshots of Web Pages
Version: 0.0.0.9000
Authors@R: person("Winston", "Chang", email = "winston@rstudio.com", role = c("aut", "cre"))
Version: 0.1.0
Authors@R: c(
person("Winston", "Chang", role = c("aut", "cre"), email = "winston@rstudio.com"),
person("Barret", "Schloerke", role = c("ctb"), email = "barret@rstudio.com", comment = c(ORCID = "0000-0001-9986-114X")),
person(given = "RStudio", role = c("cph", "fnd"))
)
Description: Takes screenshots of web pages, including Shiny applications and R
Markdown documents. Webshot2 uses headless Chrome or Chromium as the browser
backend.
Markdown documents. 'webshot2' uses headless Chrome or Chromium as the browser
back-end.
Imports:
chromote (>= 0.0.0.9001),
chromote (>= 0.1.0),
later,
promises,
magrittr,
Expand All @@ -15,13 +19,10 @@ Suggests:
shiny,
httpuv,
rmarkdown
Remotes:
rstudio/chromote
SystemRequirements: Google Chrome or other Chromium-based browser. chromium: chromium (rpm) or chromium-browser (deb). TODO Remove this field once chromote is on CRAN
Depends: R (>= 3.2)
License: GPL-2
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.1.2
RoxygenNote: 7.2.0
URL: https://github.com/rstudio/webshot2
BugReports: https://github.com/rstudio/webshot2/issues
Language: en-US
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# webshot2 0.1.0

* Added a `NEWS.md` file to track changes to the package.
3 changes: 2 additions & 1 deletion R/appshot.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#' supplied (in \code{...}), the delay value is added to the timeout value.
#'
#' @param ... Other arguments to pass on to \code{\link{webshot}}.
#' @template webshot-return
#'
#' @rdname appshot
#' @examples
Expand Down Expand Up @@ -128,7 +129,7 @@ appshot.shiny.appobj <- function(
})

# add a delay to the webshot_timeout if it exists
if(!is.null(args$delay)) {
if (!is.null(args$delay)) {
webshot_timeout <- webshot_timeout + args$delay
}
start_time <- as.numeric(Sys.time())
Expand Down
57 changes: 35 additions & 22 deletions R/image.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
#' @param filename Character vector containing the path of images to resize.
#' @param geometry Scaling specification. Can be a percent, as in \code{"50\%"},
#' or pixel dimensions like \code{"120x120"}, \code{"120x"}, or \code{"x120"}.
#' Any valid ImageMagick geometry specifation can be used. If \code{filename}
#' Any valid ImageMagick geometry specification can be used. If \code{filename}
#' contains multiple images, this can be a vector to specify distinct sizes
#' for each image.
#' @return The `filename` supplied but with a class value of `"webshot"`.
#'
#' @examples
#' if (interactive()) {
Expand Down Expand Up @@ -60,7 +61,7 @@ resize_one <- function(filename, geometry) {
res <- system2(prog, args)

if (res != 0)
stop ("Resizing with `gm convert`, `magick convert` or `convert` failed.")
stop("Resizing with `gm convert`, `magick convert` or `convert` failed.")

filename
}
Expand All @@ -77,6 +78,7 @@ resize_one <- function(filename, geometry) {
#'
#' @param filename Character vector containing the path of images to resize.
#' Must be PNG files.
#' @return The `filename` supplied but with a class value of `"webshot"`.
#'
#' @examples
#' if (interactive()) {
Expand All @@ -97,46 +99,57 @@ shrink_one <- function(filename) {
if (optipng == "")
stop("optipng not found in path. optipng must be installed and in path.")

res <- system2('optipng', filename)
res <- system2("optipng", filename)

if (res != 0)
stop ("Shrinking with `optipng` failed.")
stop("Shrinking with `optipng` failed.")

filename
}


# Borrowed from animation package, with some adaptations.
find_magic = function() {
find_magic <- function() {
# try to look for ImageMagick in the Windows Registry Hive, the Program Files
# directory and the LyX installation
if (!inherits(try({
magick.path = utils::readRegistry('SOFTWARE\\ImageMagick\\Current')$BinPath
}, silent = TRUE), 'try-error')) {
if (nzchar(magick.path)) {
convert = normalizePath(file.path(magick.path, 'convert.exe'), "/", mustWork = FALSE)
magick_path <- utils::readRegistry("SOFTWARE\\ImageMagick\\Current")$BinPath
}, silent = TRUE), "try-error")) {
if (nzchar(magick_path)) {
convert <- normalizePath(
file.path(magick_path, "convert.exe"), "/",
mustWork = FALSE
)
}
} else if (
nzchar(prog <- Sys.getenv('ProgramFiles')) &&
length(magick.dir <- list.files(prog, '^ImageMagick.*')) &&
length(magick.path <- list.files(file.path(prog, magick.dir), pattern = '^convert\\.exe$',
full.names = TRUE, recursive = TRUE))
nzchar(prog <- Sys.getenv("ProgramFiles")) &&
length(magick_dir <- list.files(prog, "^ImageMagick.*")) &&
length(magick_path <- list.files(
file.path(prog, magick_dir), pattern = "^convert\\.exe$",
full.names = TRUE, recursive = TRUE))
) {
convert = normalizePath(magick.path[1], "/", mustWork = FALSE)
convert <- normalizePath(magick_path[1], "/", mustWork = FALSE)
} else if (!inherits(try({
magick.path = utils::readRegistry('LyX.Document\\Shell\\open\\command', 'HCR')
}, silent = TRUE), 'try-error')) {
convert = file.path(dirname(gsub('(^\"|\" \"%1\"$)', '', magick.path[[1]])), c('..', '../etc'),
'imagemagick', 'convert.exe')
convert = convert[file.exists(convert)]
magick_path <- utils::readRegistry(
"LyX.Document\\Shell\\open\\command",
"HCR"
)
}, silent = TRUE), "try-error")) {
convert <- file.path(
dirname(gsub("(^\"|\" \"%1\"$)", "", magick_path[[1]])),
c("..", "../etc"),
"imagemagick",
"convert.exe"
)
convert <- convert[file.exists(convert)]
if (length(convert)) {
convert = normalizePath(convert, "/", mustWork = FALSE)
convert <- normalizePath(convert, "/", mustWork = FALSE)
} else {
warning('No way to find ImageMagick!')
warning("No way to find ImageMagick!")
return("")
}
} else {
warning('ImageMagick not installed yet!')
warning("ImageMagick not installed yet!")
return("")
}

Expand Down
16 changes: 11 additions & 5 deletions R/rmdshot.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
#' @param rmd_args A list of additional arguments to pass to either
#' \code{\link[rmarkdown]{render}} (for static Rmd documents) or
#' \code{\link[rmarkdown]{run}} (for Rmd documents with runtime:shiny).
#' @template webshot-return
#'
#' @examples
#' if (interactive()) {
#' # rmdshot("rmarkdown_file.Rmd", "snapshot.png")
#'
#' # R Markdown file
#' input_file <- system.file("examples/knitr-minimal.Rmd", package = "knitr")
#' rmdshot(input_file, "minimal_rmd.png")
Expand All @@ -27,8 +26,15 @@
#' }
#'
#' @export
rmdshot <- function(doc, file = "webshot.png", ..., delay = NULL, rmd_args = list(),
port = getOption("shiny.port"), envvars = NULL) {
rmdshot <- function(
doc,
file = "webshot.png",
...,
delay = NULL,
rmd_args = list(),
port = getOption("shiny.port"),
envvars = NULL
) {

runtime <- rmarkdown::yaml_front_matter(doc)$runtime

Expand Down Expand Up @@ -78,6 +84,6 @@ rmdshot_shiny <- function(doc, file, ..., rmd_args, port, envvars) {


# Borrowed from rmarkdown
is_shiny <- function (runtime) {
is_shiny <- function(runtime) {
!is.null(runtime) && grepl("^shiny", runtime)
}
4 changes: 2 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

is_windows <- function() .Platform$OS.type == "windows"

is_mac <- function() Sys.info()[['sysname']] == 'Darwin'
is_mac <- function() Sys.info()[["sysname"]] == "Darwin"

is_linux <- function() Sys.info()[['sysname']] == 'Linux'
is_linux <- function() Sys.info()[["sysname"]] == "Linux"



Expand Down
13 changes: 7 additions & 6 deletions R/wait.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,20 @@ wait_until_server_exists <- function(
as.numeric(Sys.time())
}
start <- cur_time()
while(!server_exists(url)) {
while (!server_exists(url)) {
if (cur_time() - start > timeout) {
stop(
'It took more than ', timeout, ' seconds to launch the Shiny Application. ',
'There may be something wrong. The process has been killed. ',
'If the app needs more time to be launched, set ',
'options(webshot.app.timeout) to a larger value.',
"It took more than ", timeout,
" seconds to launch the Shiny Application. ",
"There may be something wrong. The process has been killed. ",
"If the app needs more time to be launched, set ",
"options(webshot.app.timeout) to a larger value.",
call. = FALSE
)
}

# Check if there was a failure in starting app server
if(!p$is_alive()){
if (!p$is_alive()) {
stop(
"App has failed with error(s):\n",
paste(p$read_error_lines(), collapse = "\n"),
Expand Down
13 changes: 7 additions & 6 deletions R/webshot.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ NULL
#' This can be the string \code{"viewport"}, in which case the clipping
#' rectangle matches the viewport size, or it can be a four-element numeric
#' vector specifying the left, top, width, and height. (Note that the order of
#' left and top is reversed from the original webshot package.) When taking
#' screenshots of multiple URLs, this parameter can also be a list with same
#' left and top is reversed from the original webshot package.) When taking
#' screenshots of multiple URLs, this parameter can also be a list with same
#' length as \code{url} with each element of the list being "viewport" or a
#' four-elements numeric vector. This option is not compatible with
#' four-elements numeric vector. This option is not compatible with
#' \code{selector}.
#' @param delay Time to wait before taking screenshot, in seconds. Sometimes a
#' longer delay is needed for all assets to display properly.
Expand All @@ -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)
#' @template webshot-return
#'
#' @examples
#' if (interactive()) {
Expand Down Expand Up @@ -138,9 +139,9 @@ webshot <- function(

# Convert params cliprect, selector and expand to list if necessary, because
# they can be vectors.
if(!is.null(cliprect) && !is.list(cliprect)) cliprect <- list(cliprect)
if(!is.null(selector) && !is.list(selector)) selector <- list(selector)
if(!is.null(expand) && !is.list(expand)) expand <- list(expand)
if (!is.null(cliprect) && !is.list(cliprect)) cliprect <- list(cliprect)
if (!is.null(selector) && !is.list(selector)) selector <- list(selector)
if (!is.null(expand) && !is.list(expand)) expand <- list(expand)

if (is.null(selector)) {
selector <- "html"
Expand Down
81 changes: 81 additions & 0 deletions cran-comments.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
## Comments

#### 2022-05-13

Updates:
* I have added `\value` docs for all five methods.
* I have removed the commented code in the example of `rmdshot.Rd`

Thank you,
Winston


#### 2022-05-13

Thanks,

Please add \value to .Rd files regarding exported methods and explain
the functions results in the documentation. Please write about the
structure of the output (class) and also what the output means. (If a
function does not return a value, please document that too, e.g.
\value{No return value, called for side effects} or similar)
Missing Rd-tags:
appshot.Rd: \value
resize.Rd: \value
rmdshot.Rd: \value
shrink.Rd: \value
webshot.Rd: \value

Some code lines in examples are commented out in rmdshot.Rd
Please never do that. Ideally find toy examples that can be regularly
executed and checked. Lengthy examples (> 5 sec), can be wrapped in
\donttest{}.
If you use external software for examples, please wrap them in
\dontrun{} instead.

Please fix and resubmit.

Best,
Victoria Wimmer

#### 2022-05-12

Releasing a new package `{webshot2}`.

The invalid url to https://www.r-pkg.org/pkg/webshot2 should work once the package is on CRAN.

Please let me know if there is any more information I can provide.

Thank you,
Winston


## Test environments

* local macOS, R 4.1.3
* GitHub Actions
* macOS
* 4.2
* windows
* 4.2
* ubuntu18
* devel, 4.2, 4.1, 4.0, 3.6, 3.5
* devtools::
* check_win_devel()
* check_win_release()
* check_win_oldrelease()

## R CMD check results

0 errors ✔ | 0 warnings ✔ | 1 note

N checking CRAN incoming feasibility
Maintainer: ‘Winston Chang <winston@rstudio.com>

New submission

Found the following (possibly) invalid URLs:
URL: https://www.r-pkg.org/pkg/webshot2
From: README.md
Status: 500
Message: Internal Server Error
2 changes: 2 additions & 0 deletions man-roxygen/webshot-return.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#' @return Invisibly returns the normalized path to all screenshots taken. The
#' character vector will have a class of `"webshot"`.
4 changes: 4 additions & 0 deletions man/appshot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ce1ffab

Please sign in to comment.