From c49825a900bbfc0616c5ec1b41ef2a86f8661adb Mon Sep 17 00:00:00 2001 From: "Lewis A. Jones" <41071747+LewisAJones@users.noreply.github.com> Date: Thu, 29 Jun 2023 13:51:06 +0200 Subject: [PATCH 1/3] Add preview argument --- NEWS.md | 1 + R/get_phylopic.R | 13 ++++++++++++- man/get_phylopic.Rd | 5 ++++- tests/testthat/test-get_phylopic.R | 1 + 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index 9ef5d05e..36bc031a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,7 @@ rphylopic (development version) ============== +* added preview argument to get_phylopic (#59) * added functions for transforming PhyloPic silhouettes (flipping and rotating) * save_phylopic bg argument updated to be "transparent" by default * added geom_phylopic (#25) diff --git a/R/get_phylopic.R b/R/get_phylopic.R index 72567102..5ecb6415 100644 --- a/R/get_phylopic.R +++ b/R/get_phylopic.R @@ -17,6 +17,8 @@ #' @param height \code{numeric}. If `format` is "raster", this is the desired #' height of the raster image in pixels. This is ignored if `format` is #' "vector". +#' @param preview \code{logical}. If `preview` is `TRUE`, the returned +#' image is plotted. Defaults to `FALSE`. #' @return If `format` is "vector", a [Picture][grImport2::Picture-class] object #' is returned. If `format` is "raster", a png array representing the #' rasterized image is returned. Either way, the uuid and download url are @@ -31,7 +33,8 @@ #' # Get data for an image #' img_svg <- get_phylopic(uuid, format = "vector") # vector format #' img_png <- get_phylopic(uuid, format = "raster") # raster format -get_phylopic <- function(uuid = NULL, format = "vector", height = 512) { +get_phylopic <- function(uuid = NULL, format = "vector", height = 512, + preview = FALSE) { # Error handling ------------------------------------------------------- if (is.null(uuid)) { stop("A `uuid` is required (hint: use `get_uuid()`).") @@ -42,6 +45,9 @@ get_phylopic <- function(uuid = NULL, format = "vector", height = 512) { if (!is.character(uuid)) { stop("`uuid` is not of class character.") } + if (!is.logical(preview)) { + stop("`preview` is not of class logical.") + } if (is.numeric(format) || grepl("^[[:digit:]]+$", as.character(format))) { lifecycle::deprecate_warn("1.1.0", paste0("get_phylopic(format = '", @@ -70,6 +76,11 @@ get_phylopic <- function(uuid = NULL, format = "vector", height = 512) { url <- image_info$vectorFile$href img <- get_svg(url) } + # Should the image be previewed? + if (preview) { + add_phylopic_base(img = img, x = 0.5, y = 0.5) + } + attr(img, "uuid") <- uuid attr(img, "url") <- url img diff --git a/man/get_phylopic.Rd b/man/get_phylopic.Rd index 47025163..98d5b921 100644 --- a/man/get_phylopic.Rd +++ b/man/get_phylopic.Rd @@ -4,7 +4,7 @@ \alias{get_phylopic} \title{Retrieve an image for a given PhyloPic uuid} \usage{ -get_phylopic(uuid = NULL, format = "vector", height = 512) +get_phylopic(uuid = NULL, format = "vector", height = 512, preview = FALSE) } \arguments{ \item{uuid}{\code{character}. A PhyloPic image uuid.} @@ -16,6 +16,9 @@ a desired \code{height}.} \item{height}{\code{numeric}. If \code{format} is "raster", this is the desired height of the raster image in pixels. This is ignored if \code{format} is "vector".} + +\item{preview}{\code{logical}. If \code{preview} is \code{TRUE}, the returned +image is plotted. Defaults to \code{FALSE}.} } \value{ If \code{format} is "vector", a \link[grImport2:Picture-class]{Picture} object diff --git a/tests/testthat/test-get_phylopic.R b/tests/testthat/test-get_phylopic.R index 3ac0cd91..a3479636 100644 --- a/tests/testthat/test-get_phylopic.R +++ b/tests/testthat/test-get_phylopic.R @@ -13,6 +13,7 @@ test_that("get_phylopic works", { expect_error(get_phylopic(uuid = c("1", 2))) expect_error(get_phylopic(uuid = NULL)) expect_error(get_phylopic(uuid = uuid, format = "VHS")) + expect_error(get_phylopic(uuid = uuid, preview = "TRUE")) # 512 was deprecated for format expect_warning(get_phylopic(uuid = "c8f71c27-71db-4b34-ac2d-e97fea8762cf", From b5af385366ddf1eecd13ec8c4aba06272141db8f Mon Sep 17 00:00:00 2001 From: Will Gearty Date: Fri, 14 Jul 2023 10:20:58 -0400 Subject: [PATCH 2/3] Move news to dev version --- NEWS.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index a84b9ae4..5cc453f7 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # rphylopic (development version) +* added preview argument to get_phylopic (#59) + # rphylopic 1.1.1 * Minor fixes for Fedora @@ -7,7 +9,6 @@ # rphylopic 1.1.0 -* added preview argument to get_phylopic (#59) * added functions for transforming PhyloPic silhouettes (flipping and rotating) * save_phylopic bg argument updated to be "transparent" by default * added geom_phylopic (#25) From 9be6ff78b124482ea7ad7db0f20331ef97277909 Mon Sep 17 00:00:00 2001 From: Will Gearty Date: Fri, 14 Jul 2023 10:21:49 -0400 Subject: [PATCH 3/3] Fix preview plotting --- R/get_phylopic.R | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/R/get_phylopic.R b/R/get_phylopic.R index 5ecb6415..1fc41a88 100644 --- a/R/get_phylopic.R +++ b/R/get_phylopic.R @@ -25,6 +25,8 @@ #' included as the "uuid" and "url" attributes, respectively. #' @importFrom rsvg rsvg_png #' @importFrom png readPNG +#' @importFrom grid grid.newpage grid.raster +#' @importFrom grImport2 grid.picture #' @export #' @examples #' # uuid @@ -78,7 +80,12 @@ get_phylopic <- function(uuid = NULL, format = "vector", height = 512, } # Should the image be previewed? if (preview) { - add_phylopic_base(img = img, x = 0.5, y = 0.5) + grid.newpage() + if (format == "vector") { + grid.picture(img) + } else { + grid.raster(img) + } } attr(img, "uuid") <- uuid