automate input to user query in R

498 Views Asked by At

I apologize if this question has been asked with terminology I don't recognize but it doesn't appear to be.

I am using the function comm2sci in the library taxize to search for the scientific names for a database of over 120,000 rows of common names. Here is a subset of 10:

commnames <- c("WESTERN CAPERCAILLIE", "AARDVARK", "AARDWOLF", "ABACO ISLAND BOA", 
"ABBOTT'S DAY GECKO", "ABDIM'S STORK", "ABRONIA GRAMINEA", "ABYSSINIAN BLUE 
WINGED GOOSE", 
"ABYSSINIAN CAT", "ABYSSINIAN GROUND HORNBILL")

When searching with the NCBI database in this function, it asks for user input if the common name is generic/general and not species specific, for example the following call will ask for clarification for "AARDVARK" by entering '1', '2' or 'return' for 'NA'.

install.packages("taxize")
library(taxize)
ncbioutput <- comm2sci(commnames, db = "ncbi")###querying ncbi database

Because of this, I cannot rely on this function to find the names of the 120000 species without me sitting and entering 'return' every few minutes. I know this question sounds taxize specific - but I've had this situation in the past with other functions as well. My question is: is there a general way to place the comm2sci call in a conditional statement that will return a specific value when user input is prompted? Or otherwise write a function that will return some input when prompted?

All searches related to this tell me how to ask for user input but not how to override user queries. These are two of the question threads I've found, but I can't seem to apply them to my situation: Make R wait for console input?, Switch R script from non-interactive to interactive

I hope this was clear. Thank you very much for your time!

1

There are 1 best solutions below

5
On BEST ANSWER

So the get_* functions, used internally, all by default ask for user input when there is > 1 option. But, all of those functions have a sister function with an underscore, e.g., get_uid_ that do not prompt for input, and return all data. You can use that to get all the data, then process however you like.

Made some changes to comm2sci, so update first: devtools::install_github("ropensci/taxize")

Here's an example.

library(taxize)
commnames <- c("WESTERN CAPERCAILLIE", "AARDVARK", "AARDWOLF", "ABACO ISLAND BOA", 
               "ABBOTT'S DAY GECKO", "ABDIM'S STORK", "ABRONIA GRAMINEA", 
               "ABYSSINIAN BLUE WINGED GOOSE", 
               "ABYSSINIAN CAT", "ABYSSINIAN GROUND HORNBILL")

Then use get_uid_ to get all data

ids <- get_uid_(commnames)

Process the results in ids as you like. Here, for brevity, we'll just grab first row of each

ids <- lapply(ids, function(z) z[1,])

Then grab the uid's out

ids <- as.uid(unname(vapply(ids, "[[", "", "uid")), check = FALSE)

And pass to comm2sci

comm2sci(ids)

$`100830`
[1] "Tetrao urogallus"

$`9818`
[1] "Orycteropus afer"

$`9680`
[1] "Proteles cristatus"

$`51745`
[1] "Chilabothrus exsul"

$`8565`
[1] "Gekko"

$`39789`
[1] "Ciconia abdimii"

$`278977`
[1] "Abronia graminea"

$`8865`
[1] "Cyanochen cyanopterus"

$`9685`
[1] "Felis catus"

$`153643`
[1] "Bucorvus abyssinicus"

Note that NCBI returns common names from get_uid/get_uid_, so you can just go ahead and pluck those out if you want