Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix attribution display in pick_phylopic #105

Merged
merged 4 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Encoding: UTF-8
LazyData: true
biocViews:
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Depends:
R (>= 4.0)
Imports:
Expand Down
16 changes: 12 additions & 4 deletions R/pick_phylopic.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ utils::globalVariables(c("x", "y", "uuid", "label"))
#' @param auto \code{numeric}. This argument allows the user to automate input
#' into the menu choice. If the input value is `1`, the first returned image
#' will be selected. If the input value is `2`, requested images will be
#' automatically cycled through with the final image returned. If `NULL`
#' (default), the user must interactively respond to the called menu.
#' automatically cycled through with the final image returned. If the input
#' value is `3`, a list of attribution information for each image is
#' returned (this functionality is principally intended for testing). If
#' `NULL` (default), the user must interactively respond to the called menu.
#'
#' @return A [Picture][grImport2::Picture-class] object is returned. The uuid of
#' the selected image is saved as the "uuid" attribute of the returned object
Expand Down Expand Up @@ -60,8 +62,8 @@ utils::globalVariables(c("x", "y", "uuid", "label"))
pick_phylopic <- function(name = NULL, n = 5, uuid = NULL, view = 1,
filter = NULL, auto = NULL) {
# Error handling
if (!is.null(auto) && !auto %in% c(1, 2)) {
stop("`auto` must be of value: NULL, 1, or 2")
if (!is.null(auto) && !auto %in% c(1, 2, 3)) {
stop("`auto` must be of value: NULL, 1, 2, or 3")
}
if (!is.numeric(view)) {
stop("`view` must be of class numeric.")
Expand All @@ -75,8 +77,10 @@ pick_phylopic <- function(name = NULL, n = 5, uuid = NULL, view = 1,
grid.newpage()
grid.picture(img)
# Add text for attribution
att <- att[[1]][[1]]
att_string <- paste0("Contributor: ", att$contributor, "\n",
"Created: ", att$created, "\n",
"Attribution: ", att$attribution, "\n",
"License: ", att$license)
grid.text(label = att_string,
x = 0.96, y = 0.92,
Expand Down Expand Up @@ -126,6 +130,7 @@ pick_phylopic <- function(name = NULL, n = 5, uuid = NULL, view = 1,
}
# Get attribution data
att <- lapply(uuids[[i]], get_attribution)
att <- lapply(att, function(x) x[[1]][[1]])
# Attribution text
n_spaces <- 3 + floor(log10(length(att) + 1))
att_string <- lapply(att, function(x) {
Expand Down Expand Up @@ -174,6 +179,9 @@ pick_phylopic <- function(name = NULL, n = 5, uuid = NULL, view = 1,
m <- n_plotted + 1
} else if (auto == 1) {
m <- 1
} else if (auto == 3) {
names(att) <- sapply(att, function(x) x$image_uuid)
return(att)
}
}

Expand Down
6 changes: 4 additions & 2 deletions man/pick_phylopic.Rd

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

5 changes: 5 additions & 0 deletions tests/testthat/test-pick_phylo.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ test_that("pick_phylopic works", {
expect_true(is(pick_phylopic(name = "Bacteria", n = 13,
view = 5, auto = 2),
"Picture"))

# Test attribution information
expect_equal(
length(pick_phylopic(name = "Scleractinia", n = 4, view = 4, auto = 3)),
4)

# Expect warning
expect_warning(pick_phylopic(name = "Acropora cervicornis", n = 10))
Expand Down
Loading