This how the Ruby code does it, translated to R, uses RCurl
and XML
:
> xmlresponse = xmlParse( getURL("http://www.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pccompound&retmax=100&term=64318-79-2") )
and here's how to extract the Id nodes:
> sapply(xpathSApply(xmlresponse, "//Id"), function(n){xmlValue(n)})
[1] "6434870" "5282237"
wrap all that in a function....
convertU = function(query){
xmlresponse = xmlParse(getURL(
paste0("http://www.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pccompound&retmax=100&term=",query)))
sapply(xpathSApply(xmlresponse, "//Id"), function(n){xmlValue(n)})
}
> convertU("64318-79-2")
[1] "6434870" "5282237"
> convertU("64318-79-1")
list()
> convertU("64318-78-2")
list()
> convertU("64313-78-2")
[1] "313"
maybe needs a test if not found.