When using phylopic_uid, sometimes it will fail with error Error in jsonlite::fromJSON(url)$result[[1]] : subscript out of bounds whenever Phylopic doesn't have the species in their database. For a small set of species names, troubleshooting this isn't too hard, but for longer species lists, figuring out which species are causing the problem can add up to a significant timesink.
Specifically, the problem lies in the ggimage internal function phylopic_uid_item, and its assumption that you will always get at least one item from jsonlite::fromJSON(url)$result. It would be safer to first save the object to a variable, and then check for the length before proceeding.
My proposed fix would be to add an if-else statement checking the length of the results item as follows:
phylopic_uid_item <- function (name)
{
x <- gsub("[^a-zA-Z]+", "+", name)
url <- paste0("http://phylopic.org/api/a/name/search?text=",
x, "&options=scientific+json")
results <- jsonlite::fromJSON(url)$result
if (length(results)==0){
return(NA)
} else{
res <- results[[1]]
for (id in res$uid) {
uid <- phylopic_valid_id(id)
if (!is.na(uid))
break
}
return(uid)}
}
As a minimal test case, you can compare the output of phylopic_uid("Procavia_capensis") (which always works) with phylopic_uid("Chlorocebus_sabaeus") (which fails currently, but returns NA with my solution.
If this works well for you, then I can also submit a pull request with the change.
Edit:
Additionally, this fix would require changing "vapply" to "sapply" in phylopic_uid to allow NA values.
When using
phylopic_uid, sometimes it will fail with errorError in jsonlite::fromJSON(url)$result[[1]] : subscript out of boundswhenever Phylopic doesn't have the species in their database. For a small set of species names, troubleshooting this isn't too hard, but for longer species lists, figuring out which species are causing the problem can add up to a significant timesink.Specifically, the problem lies in the
ggimageinternal functionphylopic_uid_item, and its assumption that you will always get at least one item from jsonlite::fromJSON(url)$result. It would be safer to first save the object to a variable, and then check for the length before proceeding.My proposed fix would be to add an if-else statement checking the length of the results item as follows:
As a minimal test case, you can compare the output of
phylopic_uid("Procavia_capensis")(which always works) withphylopic_uid("Chlorocebus_sabaeus")(which fails currently, but returns NA with my solution.If this works well for you, then I can also submit a pull request with the change.
Edit:
Additionally, this fix would require changing "vapply" to "sapply" in
phylopic_uidto allow NA values.