## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(
  message = FALSE,
  warning = FALSE,
  comment = "#>"
)

## ----libs---------------------------------------------------------------------
library(puremoe)
library(dplyr)
library(DT)

## ----search-------------------------------------------------------------------
pmids <- puremoe::search_pubmed('("political ideology"[TiAb])')
length(pmids)

## ----subset-------------------------------------------------------------------
pmids_sub <- head(pmids, 50L)

## ----abstracts----------------------------------------------------------------
abstracts <- puremoe::get_records(
  pmids_sub,
  endpoint = "pubmed_abstracts",
  cores    = 1L,
  sleep    = 0.5
)

abstracts <- abstracts |> mutate(pmid = as.character(pmid))

## ----abstracts-table----------------------------------------------------------
abstracts |>
  select(pmid, year, journal, articletitle) |>
  DT::datatable(rownames = FALSE)

## ----annotations--------------------------------------------------------------
bind_rows(abstracts$annotations) |>
  head(20) |>
  DT::datatable(rownames = FALSE)

## ----affiliations-------------------------------------------------------------
affiliations <- puremoe::get_records(
  head(pmids_sub, 25L),
  endpoint = "pubmed_affiliations",
  cores    = 1L,
  sleep    = 0.5
)

affiliations |>
  DT::datatable(rownames = FALSE)

## ----icites-------------------------------------------------------------------
icites <- puremoe::get_records(
  pmids_sub,
  endpoint = "icites",
  cores    = 1L,
  sleep    = 0.25
)

icites |>
  mutate(pmid = as.character(pmid)) |>
  select(-citation_net, -cited_by_clin) |>
  DT::datatable(rownames = FALSE, options = list(scrollX = TRUE))

## ----pubtations---------------------------------------------------------------
pubtations <- puremoe::get_records(
  head(pmids_sub, 30L),
  endpoint = "pubtations",
  cores    = 1L
)

pubtations |>
  DT::datatable(rownames = FALSE)

## ----ftp----------------------------------------------------------------------
ftp <- puremoe::pmid_to_ftp(pmids = pmids_sub)
ftp |> DT::datatable(rownames = FALSE, options = list(scrollX = TRUE))

## ----fulltext-----------------------------------------------------------------
fulltext <- puremoe::get_records(
  head(ftp$url, 2L),
  endpoint = "pmc_fulltext",
  cores    = 1L
)

fulltext |>
  mutate(text = sapply(strsplit(text, "\\s+"), function(w) paste0(paste(head(w, 15), collapse = " "), "..."))) |>
  slice(1:5) |>
  DT::datatable(rownames = FALSE, options = list(scrollX = TRUE))

## ----endpoint-info------------------------------------------------------------
puremoe::endpoint_info()

## ----endpoint-detail----------------------------------------------------------
puremoe::endpoint_info("icites")

