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

phylopic_uid & phylopic_uid_item return "subscript out of bounds" errors when a species is not found #24

Open
docmanny opened this issue Apr 12, 2020 · 1 comment

Comments

@docmanny
Copy link

docmanny commented Apr 12, 2020

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.

@GuangchuangYu
Copy link
Owner

GuangchuangYu commented Apr 13, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants