Skip to content

Commit fe5d699

Browse files
authored
Add get_phylopic() option to use original source file from PhyloPic (#117)
* Add get_phylopic() option to use original source file from PhyloPic * Fix get_phylopic() usage in pick_phylopic() * Add tests for source files * Add to NEWS
1 parent 6363e0f commit fe5d699

File tree

6 files changed

+58
-12
lines changed

6 files changed

+58
-12
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ importFrom(rsvg,rsvg_png)
9393
importFrom(rsvg,rsvg_svg)
9494
importFrom(scales,pal_area)
9595
importFrom(stats,setNames)
96+
importFrom(tools,file_ext)
9697
importFrom(utils,URLdecode)
9798
importFrom(utils,URLencode)
9899
importFrom(utils,browseURL)

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Fixed a bug in add_phylopic_base() where all names were reported as not returning PhyloPic results when only a single name actually returned no PhyloPic results
55
* resolve_phylopic() now will retry API calls if they fail
66
* Fixed geom_phylopic() and add_phylopic() under ggplot2 4.0.0 and up (#125)
7+
* get_phylopic() now has a "source" argument that can be used to retrieve the original source file from the PhyloPic database (#116)
78

89
# rphylopic 1.5.0
910

R/get_phylopic.R

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
#' @param format \code{character}. Format of the image. To return a vectorized
1515
#' image, use "vector". To return a rasterized image, use "raster" and specify
1616
#' a desired `height`.
17+
#' @param source \code{logical}. If `FALSE` (the default), the vector file
18+
#' generated by PhyloPic is used to generate the image in the desired format.
19+
#' If `TRUE`, the original source file is used. Note that only PNG and SVG
20+
#' source files are supported for the "raster" `format` and only SVG source
21+
#' files are supported for the "vector" `format`.
1722
#' @param height \code{numeric}. If `format` is "raster", this is the desired
1823
#' height of the raster image in pixels. This is ignored if `format` is
1924
#' "vector".
@@ -25,6 +30,7 @@
2530
#' included as the "uuid" and "url" attributes, respectively.
2631
#' @importFrom grid grid.newpage grid.raster
2732
#' @importFrom grImport2 grid.picture
33+
#' @importFrom tools file_ext
2834
#' @export
2935
#' @examples \dontrun{
3036
#' # uuid
@@ -34,8 +40,8 @@
3440
#' img_svg <- get_phylopic(uuid, format = "vector") # vector format
3541
#' img_png <- get_phylopic(uuid, format = "raster") # raster format
3642
#' }
37-
get_phylopic <- function(uuid = NULL, format = "vector", height = 512,
38-
preview = FALSE) {
43+
get_phylopic <- function(uuid = NULL, format = "vector", source = FALSE,
44+
height = 512, preview = FALSE) {
3945
# Error handling -------------------------------------------------------
4046
if (is.null(uuid)) {
4147
stop("A `uuid` is required (hint: use `get_uuid()`).")
@@ -60,22 +66,43 @@ get_phylopic <- function(uuid = NULL, format = "vector", height = 512,
6066
format <- "raster"
6167
}
6268
format <- match.arg(as.character(format), c("raster", "vector"))
69+
if (!is.logical(source)) {
70+
stop("`source` is not of class logical.")
71+
}
6372
image_info <- phy_GET(file.path("images", uuid))$`_links`
6473
if (format == "raster") { # get raster
65-
rasters <- image_info$rasterFiles
66-
# check if there is an existing file with the desired height
67-
ind <- grepl(paste0("x", height), rasters$sizes)
68-
if (any(ind)) {
69-
url <- rasters$href[ind]
70-
img <- get_png(url)
74+
if (source) {
75+
url <- image_info$sourceFile$href
7176
} else {
72-
url <- image_info$vectorFile$href
77+
rasters <- image_info$rasterFiles
78+
# check if there is an existing file with the desired height
79+
ind <- grepl(paste0("x", height), rasters$sizes)
80+
if (any(ind)) {
81+
url <- rasters$href[ind]
82+
} else {
83+
url <- image_info$vectorFile$href
84+
}
85+
}
86+
ext <- file_ext(url)
87+
if (ext == "png") {
88+
img <- get_png(url)
89+
} else if (ext == "svg") {
7390
# use the svg to make a png with the desired height
7491
img <- make_png(url, height)
92+
} else {
93+
stop("Source file is not a PNG or SVG. Can not generate a raster image.")
7594
}
7695
class(img) <- c("phylopic", class(img))
7796
} else if (format == "vector") { # get vector
78-
url <- image_info$vectorFile$href
97+
if (source) {
98+
url <- image_info$sourceFile$href
99+
ext <- file_ext(url)
100+
if (ext != "svg") { # I think this is possible now?
101+
stop("Source file is not an SVG. Can not generate a vector image.")
102+
}
103+
} else {
104+
url <- image_info$vectorFile$href
105+
}
79106
img <- get_svg(url)
80107
}
81108
# Should the image be previewed?

R/pick_phylopic.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ pick_phylopic <- function(name = NULL, n = 5, uuid = NULL, view = 1,
124124
# Get image data
125125
height <- 1024 / ceiling(sqrt(view))
126126
if (view > 1 && length(uuids[[i]]) > 1) {
127-
img <- pblapply(uuids[[i]], get_phylopic, format = "raster", height)
127+
img <- pblapply(uuids[[i]], get_phylopic, format = "raster",
128+
height = height)
128129
} else {
129130
img <- sapply(uuids[[i]], get_phylopic)
130131
}

man/get_phylopic.Rd

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-get_phylopic.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ test_that("get_phylopic works", {
88
expect_true(is(get_phylopic(uuid = uuid, format = "raster"), "array"))
99
expect_true(is(get_phylopic(uuid = uuid, format = "raster",
1010
height = 300), "array"))
11+
expect_true(is(get_phylopic(uuid = uuid, format = "vector", source = TRUE),
12+
"Picture"))
13+
expect_true(is(get_phylopic(uuid = uuid, format = "raster", source = TRUE),
14+
"array"))
1115

1216
expect_no_error(get_phylopic(uuid = uuid, preview = TRUE))
1317

0 commit comments

Comments
 (0)