Skip to content

Commit

Permalink
Don't error when phantomjs is missing
Browse files Browse the repository at this point in the history
Packages that use webshot in their R CMD check process will no longer error
when phantomjs is missing.
  • Loading branch information
wch committed Jun 22, 2016
1 parent 24b2dd6 commit c0d7747
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
6 changes: 6 additions & 0 deletions R/image.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down Expand Up @@ -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.")
Expand Down
24 changes: 19 additions & 5 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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)
}
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions R/webshot.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
5 changes: 0 additions & 5 deletions vignettes/intro.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c0d7747

Please sign in to comment.