Last active
May 18, 2021 16:52
-
-
Save muschellij2/aa9e7a1ad3df271b7ee8c716c2439629 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
better_fetch_pubmed_data = function (pubmed_id_list, retstart = 0, retmax = 500, format = "xml", | |
encoding = "UTF8") | |
{ | |
myIDlist <- pubmed_id_list | |
if ((!is.list(myIDlist)) | is.na(myIDlist$WebEnv) | is.na(myIDlist$QueryKey) | | |
is.na(myIDlist$Count) | !is.integer(as.integer(retstart)) | | |
!is.integer(as.integer(retmax))) { | |
message("There is an issue with the PubMed ID list you supplied. Please, call the function again and supply the result of a <get_pubmed_ids()> call as argument. Thank you.") | |
return(NULL) | |
} | |
myWebEnv <- myIDlist$WebEnv | |
myKey <- myIDlist$QueryKey | |
myCount <- as.numeric(as.character(myIDlist$Count)) | |
myRetstart = as.integer(retstart) | |
if (myRetstart < 0) { | |
myRetstart = 0 | |
} | |
myRetmax <- as.integer(retmax) | |
if (myRetmax > 5000) { | |
myRetmax = 5000 | |
} | |
if (myRetmax < 1) { | |
myRetmax = 1 | |
} | |
if (format[1] %in% c("medline", "uilist", "abstract", | |
"asn.1", "xml")) { | |
myFormat <- format[1] | |
} | |
else { | |
myFormat <- "xml" | |
} | |
typeMode <- switch(myFormat, asn.1 = c("null", "asn.1"), | |
xml = c("null", "xml"), medline = c("medline", "text"), | |
uilist = c("uilist", "text"), abstract = c("abstract", | |
"text")) | |
efetch_url = paste("https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?", | |
"db=pubmed&WebEnv=", myWebEnv, "&query_key=", myKey, | |
"&retstart=", myRetstart, "&retmax=", myRetmax, "&rettype=", | |
typeMode[1], "&retmode=", typeMode[2], sep = "") | |
api_key <- pubmed_id_list$APIkey | |
if (!is.null(api_key)) { | |
efetch_url <- paste(efetch_url, "&api_key=", api_key, | |
sep = "") | |
} | |
tfile = tempfile(fileext = ".xml"); | |
xx = curl::curl_download(efetch_url, | |
destfile = tfile, quiet = FALSE) | |
return(tfile) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment