From db120d5f489ae41702037b6224c7bdd800164d14 Mon Sep 17 00:00:00 2001 From: pcastellanoescuder Date: Fri, 15 Dec 2023 14:42:05 -0500 Subject: [PATCH] annotate pubmed table --- DESCRIPTION | 3 ++- NAMESPACE | 1 + R/helper.R | 18 ++++++++++++++++++ R/tables.R | 15 +++++++++++++-- man/make_pubmed_table.Rd | 3 ++- man/pubmed_title.Rd | 14 ++++++++++++++ 6 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 man/pubmed_title.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 665407c..89a7c74 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: ddh Type: Package Title: Utility Functions and Data Sets for Data-driven Hypothesis -Version: 0.3.19 +Version: 0.3.20 Authors@R: c( person(given = "Matthew", family = "Hirschey", @@ -58,6 +58,7 @@ Imports: rayvertex, remotes, rlang, + rvest, scales, stringr, tibble, diff --git a/NAMESPACE b/NAMESPACE index 64e6b06..3aaa172 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -105,6 +105,7 @@ export(make_tissue) export(make_top_table) export(make_umap_plot) export(plot_size_finder) +export(pubmed_title) export(scale_color_ddh_c) export(scale_color_ddh_d) export(scale_fill_ddh_c) diff --git a/R/helper.R b/R/helper.R index a4c0cde..97b7cf9 100644 --- a/R/helper.R +++ b/R/helper.R @@ -889,3 +889,21 @@ good_file_namer <- function(input = list){ } return(good_file_name) } + +#' Extract PubMed Title from PubMed ID +#' +#' @param pmid PubMed ID +#' +#' @export +pubmed_title <- function(pmid) { + paper_url <- glue::glue("https://pubmed.ncbi.nlm.nih.gov/{pmid}/") + + paper_title <- rvest::read_html(paper_url) %>% + rvest::html_nodes("title") %>% + rvest::html_text() + + paper_title <- gsub(" - PubMed", "", paper_title[1]) + + return(paper_title) +} + diff --git a/R/tables.R b/R/tables.R index bba3cb1..bcf3bea 100644 --- a/R/tables.R +++ b/R/tables.R @@ -108,10 +108,11 @@ make_compound_table <- function(input = list(), #' @export #' @examples #' make_pubmed_table(input = list(type = 'gene', content = 'ROCK1')) +#' make_pubmed_table(input = list(type = 'gene', content = 'ROCK1'), title = TRUE) #' \dontrun{ #' make_pubmed_table(input = list(type = 'gene', content = 'ROCK1')) #' } -make_pubmed_table <- function(input = list()) { +make_pubmed_table <- function(input = list(), n_papers = 10, title = FALSE) { make_pubmed_table_raw <- function() { pubmed_table <- get_data_object(object_name = input$content, @@ -119,8 +120,18 @@ make_pubmed_table <- function(input = list()) { pivotwider = TRUE) %>% dplyr::mutate(year = as.numeric(year), pmid = as.numeric(pmid)) %>% - dplyr::arrange(as.numeric(pmid)) %>% + dplyr::arrange(dplyr::desc(year)) %>% + dplyr::slice(1:n_papers) %>% dplyr::select(id, pmid, year, pmcid) + + if (title) { + pubmed_table <- pubmed_table %>% + dplyr::rowwise() %>% + dplyr::mutate(Title = pubmed_title(pmid)) %>% + dplyr::ungroup() %>% + dplyr::relocate(Title, .after = pmid) + } + return(pubmed_table) } #error handling diff --git a/man/make_pubmed_table.Rd b/man/make_pubmed_table.Rd index ea65f7d..995f024 100644 --- a/man/make_pubmed_table.Rd +++ b/man/make_pubmed_table.Rd @@ -4,7 +4,7 @@ \alias{make_pubmed_table} \title{Pubmed Table} \usage{ -make_pubmed_table(input = list()) +make_pubmed_table(input = list(), n_papers = 10, title = FALSE) } \arguments{ \item{input}{Expecting a list containing type and content variable.} @@ -20,6 +20,7 @@ This is a table function that takes a gene name and returns a pubmed Table } \examples{ make_pubmed_table(input = list(type = 'gene', content = 'ROCK1')) +make_pubmed_table(input = list(type = 'gene', content = 'ROCK1'), title = TRUE) \dontrun{ make_pubmed_table(input = list(type = 'gene', content = 'ROCK1')) } diff --git a/man/pubmed_title.Rd b/man/pubmed_title.Rd new file mode 100644 index 0000000..248c5df --- /dev/null +++ b/man/pubmed_title.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/helper.R +\name{pubmed_title} +\alias{pubmed_title} +\title{Extract PubMed Title from PubMed ID} +\usage{ +pubmed_title(pmid) +} +\arguments{ +\item{pmid}{PubMed ID} +} +\description{ +Extract PubMed Title from PubMed ID +}