Skip to content

Commit

Permalink
Add plot and print methods for Picture and array objects
Browse files Browse the repository at this point in the history
  • Loading branch information
willgearty committed Aug 22, 2023
1 parent 37a7b9a commit 79b77d9
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 0 deletions.
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

S3method(flip_phylopic,Picture)
S3method(flip_phylopic,array)
S3method(plot,Picture)
S3method(plot,phylopic)
S3method(print,Picture)
S3method(print,phylopic)
S3method(recolor_phylopic,Picture)
S3method(recolor_phylopic,array)
S3method(rotate_phylopic,Picture)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* added browse_phylopic function (#60)
* added preview argument to get_phylopic (#59)
* switched to {maps} package in base R advanced vignette
* added plot and print methods for silhouette objects (#73)

# rphylopic 1.1.1

Expand Down
1 change: 1 addition & 0 deletions R/get_phylopic.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ get_phylopic <- function(uuid = NULL, format = "vector", height = 512,
# use the svg to make a png with the desired height
img <- readPNG(rsvg_png(image_info$vectorFile$href, height = height))
}
class(img) <- c("phylopic", class(img))
} else if (format == "vector") { # get vector
url <- image_info$vectorFile$href
img <- get_svg(url)
Expand Down
44 changes: 44 additions & 0 deletions R/phylopic_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,47 @@ recolor_content <- function(x, alpha, color, remove_background) {
x@content <- Filter(function(element) !is.null(element), tmp)
return(x)
}

#' Plot a vector representation of a PhyloPic silhouette
#' @param img A [Picture][grImport2::Picture-class] object, e.g., from using
#' [get_phylopic()].
#' @param ... Other arguments passed on to [grImport2::grid.picture()].
#' @importFrom grid grid.newpage
#' @importFrom grImport2 grid.picture
#' @export
plot.Picture <- function(img, ...) {
grid.newpage()
grid.picture(img, ...)
}

#' Plot a raster representation of a PhyloPic silhouette
#' @param img A png array object, e.g., from using [get_phylopic()].
#' @param ... Other arguments passed on to [grid::grid.raster()].
#' @importFrom grid grid.newpage grid.raster
#' @export
plot.phylopic <- function(img, ...) {
grid.newpage()
grid.raster(img, ...)
}

#' @export
print.Picture <- function(img) {
dims <- c(abs(diff(img@summary@xscale)), abs(diff(img@summary@yscale)))
cat(paste0("Vector representation of a PhyloPic silhouette.",
"\nDimensions: ", dims[1], " pixels wide and ",
dims[2], " pixels tall.",
"\nuuid: ", attr(img, "uuid"),
"\nURL: ", attr(img, "url")))
invisible(img)
}

#' @export
print.phylopic <- function(img) {
dims <- dim(img)
cat(paste0("Raster representation of a PhyloPic silhouette.",
"\nDimensions: ", dims[2], " pixels wide and ",
dims[1], " pixels tall.",
"\nuuid: ", attr(img, "uuid"),
"\nURL: ", attr(img, "url")))
invisible(img)
}
17 changes: 17 additions & 0 deletions man/plot.Picture.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions man/plot.phylopic.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 79b77d9

Please sign in to comment.