From c0d77478657ad4d9d4db9d83d7d61d8d75b314d2 Mon Sep 17 00:00:00 2001 From: Winston Chang Date: Wed, 22 Jun 2016 13:14:43 -0500 Subject: [PATCH] Don't error when phantomjs is missing Packages that use webshot in their R CMD check process will no longer error when phantomjs is missing. --- R/image.R | 6 ++++++ R/utils.R | 24 +++++++++++++++++++----- R/webshot.R | 3 +++ vignettes/intro.Rmd | 5 ----- 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/R/image.R b/R/image.R index 7524fe8..6297066 100644 --- a/R/image.R +++ b/R/image.R @@ -21,6 +21,9 @@ #' } #' @export resize <- function(filename, geometry) { + # Handle missing phantomjs + if (is.null(filename)) return(NULL) + progs <- Sys.which(c("gm", "convert")) if (all(progs == "")) stop("Neither `gm` nor `convert` were found in path. GraphicsMagick or ImageMagick must be installed and in path.") @@ -62,6 +65,9 @@ resize <- function(filename, geometry) { #' } #' @export shrink <- function(filename) { + # Handle missing phantomjs + if (is.null(filename)) return(NULL) + optipng <- Sys.which("optipng") if (optipng == "") stop("optipng not found in path. optipng must be installed and in path.") diff --git a/R/utils.R b/R/utils.R index e12ac6e..94347b8 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1,6 +1,9 @@ phantom_run <- function(args, wait = TRUE) { phantom_bin <- find_phantom() + # Handle missing phantomjs + if (is.null(phantom_bin)) return(NULL) + # Make sure args is a char vector args <- as.character(args) @@ -12,15 +15,25 @@ phantom_run <- function(args, wait = TRUE) { find_phantom <- function() { path <- Sys.which( "phantomjs" ) if (path != "") return(path) + for (d in phantom_paths()) { exec <- if (is_windows()) "phantomjs.exe" else "phantomjs" path <- file.path(d, exec) if (utils::file_test("-x", path)) break else path <- "" } + if (path == "") { - stop("PhantomJS not found. You can install it with webshot::install_phantomjs(). ", - "If it is installed, please make sure the phantomjs executable ", - "can be found via the PATH variable.") + # It would make the most sense to throw an error here. However, that would + # cause problems with CRAN. The CRAN checking systems may not have phantomjs + # and may not be capable of installing phantomjs (like on Solaris), and any + # packages which use webshot in their R CMD check (in examples or vignettes) + # will get an ERROR. We'll issue a message and return NULL; other + message( + "PhantomJS not found. You can install it with webshot::install_phantomjs(). ", + "If it is installed, please make sure the phantomjs executable ", + "can be found via the PATH variable." + ) + return(NULL) } path.expand(path) } @@ -125,8 +138,9 @@ dropNulls <- function(x) { } is_windows <- function() .Platform$OS.type == "windows" -is_osx <- function() Sys.info()[['sysname']] == 'Darwin' -is_linux <- function() Sys.info()[['sysname']] == 'Linux' +is_osx <- function() Sys.info()[['sysname']] == 'Darwin' +is_linux <- function() Sys.info()[['sysname']] == 'Linux' +is_solaris <- function() Sys.info()[['sysname']] == 'SunOS' # Find an available TCP port (to launch Shiny apps) available_port <- function(port) { diff --git a/R/webshot.R b/R/webshot.R index c86599c..449be75 100644 --- a/R/webshot.R +++ b/R/webshot.R @@ -141,6 +141,9 @@ webshot <- function( res <- phantom_run(args) + # Handle missing phantomjs + if (is.null(res)) return(NULL) + if (res != 0) { stop("webshot.js returned failure value: ", res) } diff --git a/vignettes/intro.Rmd b/vignettes/intro.Rmd index 9b435ba..53ed5f8 100644 --- a/vignettes/intro.Rmd +++ b/vignettes/intro.Rmd @@ -13,11 +13,6 @@ The [**webshot**](https://github.com/wch/webshot) package makes it easy to take ```{r setup, include=FALSE, warning=FALSE} library(webshot) -# Exit early if PhantomJS is not found -if (inherits(try(webshot:::find_phantom()), "try-error")) { - warning('PhantomJS is not found. This vignette will terminate from here.') - knitr::knit_exit() -} knitr::opts_chunk$set(tidy = FALSE)