From fb1d9194c56a6968fd06b38694f73d251f330d99 Mon Sep 17 00:00:00 2001 From: Jeroen Ooms Date: Sat, 20 Jul 2024 15:25:16 +0200 Subject: [PATCH] Only display warning message if reading has failed See discussion #398 --- R/edit.R | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/R/edit.R b/R/edit.R index b658a78..24a1481 100644 --- a/R/edit.R +++ b/R/edit.R @@ -65,9 +65,6 @@ image_read <- function(path, density = NULL, depth = NULL, strip = FALSE, coales density <- as.character(density) depth <- as.integer(depth) defines <- validate_defines(defines) - if(is.character(path) && grepl("\\.pdf$", path) && Sys.which('gs') == "" && !file.exists('C:\\Program Files\\gs')){ - message("Ghostscript (gs) not found on the PATH.\nTry image_read_pdf() to read PDF using pdftools instead.") - } image <- if(isS4(path) && methods::is(path, "Image")){ convert_EBImage(path) } else if(inherits(path, "nativeRaster") || (is.matrix(path) && is.integer(path))){ @@ -90,7 +87,12 @@ image_read <- function(path, density = NULL, depth = NULL, strip = FALSE, coales } else { enc2native(path) } - magick_image_readpath(path, density, depth, strip, defines) + withCallingHandlers(magick_image_readpath(path, density, depth, strip, defines), error = function(err){ + # Here we can put some additional diagnostics in case imagemagick fails + if(any(grepl("\\.pdf$", path)) && Sys.which('gs') == ""){ + message("Ghostscript (gs) not found on the PATH.\nUse: image_read_pdf() to read with pdftools.") + } + }) } else { stop("path must be URL, filename or raw vector") }