rxmlhttrpubmedpubmed-api

Working with pubmed api in R (httr) to retrieve abstracts


I am trying to work directly with the pubmed api from R using httr. There are excellent packages available such as RISmed and easypubmed but for this particular task, I need to interact directly with the api.

Using this set of instructions (https://www.ncbi.nlm.nih.gov/books/NBK25500/), I started with this code but the returned is a list without any details or pmid. Any guidance is appreciated. or if you are aware of particular tutorials on using R in this setting.

library(XML)
library(httr)
library(glue)
 
query = 'asthma[mesh]+AND+leukotrienes[mesh]+AND+2009[pdat]'
 
reqq = glue ('https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term={query}')

op = GET(reqq)

I also tried the code from this post (why i get that error : XML content does not seem to be XML), but it give this error Error in read_xml.raw(x, encoding = encoding, ...) : Opening and ending tag mismatch: meta line 17 and head [76]


Solution

  • You can read XML response and parse it to collect pubmed IDs, for instance:

    library(magrittr) 
    df_op <- op %>% xml2::read_xml() %>% xml2::as_list()
    
    pmids <- df_op$eSearchResult$IdList %>% unlist(use.names = FALSE)
    

    which gives:

    > pmids
     [1] "20113659" "20074456" "20046412" "20021457" "20008883" "20008181" "19912318" "19897276" "19895589"
    [10] "19894390" "19852204" "19839969" "19811112" "19757309" "19749079" "19739647" "19706339" "19665766"
    [19] "19648384" "19647860"