R/getInfo.R

Defines functions getInfo

Documented in getInfo

#' Get top 5 related PubMed papers use easyPubMed
#'
#' This function searches the top 5 PubMed papers related to the molecule or pathway or group you want
#'
#' @param main Characters which are the keywords you want to search
#' @param abstract Whether including the abstract
#' @import easyPubMed
#' @import XML
#' @export
#' @return A list containing the top five papers information, including title, author, abstract.
#' @examples
#' library(TestPMD)
#' getInfo("4-O-Methylmelleolide")[1:2,]


getInfo <- function(main, abstract = F){
  if(!is.character(main)){stop("main must be characters")}
  query <- main
  entrez_id <- easyPubMed::get_pubmed_ids(query)
  if(entrez_id$Count == 0){
    print("No records found.")}
  else{
    print(paste(entrez_id$Count, "records found.", sep = " "))
    abstracts_xml <- easyPubMed::fetch_pubmed_data(pubmed_id_list = entrez_id)
    my_titles <- easyPubMed::custom_grep(abstracts_xml, "ArticleTitle", "char")
    my_dates <- easyPubMed::custom_grep(abstracts_xml, "PubDate", "char")
    my_dates <- sapply(my_dates, function(x){if(is.null(easyPubMed::custom_grep(x, "Year", "char"))){easyPubMed::custom_grep(x, "MedlineDate", "char")}
      else{easyPubMed::custom_grep(x, "Year", "char")}})
    names(my_dates) <- NULL
    my_journals <- easyPubMed::custom_grep(abstracts_xml, "Title", "char")
    my_names <- easyPubMed::custom_grep(abstracts_xml, "AuthorList", "char")
    my_authors <- sapply(my_names, function(x){paste(paste(easyPubMed::custom_grep(x,"ForeName", "char"), easyPubMed::custom_grep(x,"LastName", "char"), sep = " "), collapse = ", ")})
    names(my_authors) <- NULL
    Info <- data.frame(Title = my_titles[1:5], Author = my_authors[1:5], Journal = my_journals[1:5], Date = my_dates[1:5])
    if(abstract){
      my_abstracts <- easyPubMed::custom_grep(abstracts_xml, "AbstractText", "char")
      Info$Abstract <- my_abstracts[1:5]}
    return(Info)
    }
}
YunhuiQi/TestPMD documentation built on May 5, 2022, 8:23 p.m.