## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = FALSE, comment = "")
# Console colour carries no meaning on a rendered page. pkgdown turns it on for
# its own build, and the escape sequences then reach the reader as literal text,
# so colour is switched off here for a plain vignette render and a site build
# alike. The fixed width keeps tibbles inside the documentation column.
options(cli.num_colors = 1, cli.hyperlink = FALSE, crayon.enabled = FALSE,
        width = 80)
# Print data frames and tibbles as formatted tables.
local({
  kp <- function(x, ...) {
    if (any(vapply(x, is.list, logical(1)))) return(knitr::normal_print(x))
    knitr::knit_print(knitr::kable(x))
  }
  for (cls in c("data.frame", "tbl_df", "tbl")) {
    registerS3method("knit_print", cls, kp, envir = asNamespace("knitr"))
  }
})

## ----setup--------------------------------------------------------------------
library(scopusflow)

## ----eval = FALSE-------------------------------------------------------------
# recs <- scopus_fetch("DOI(10.1038/nature14539)", view = "COMPLETE")
# recs$authkeywords

## ----include = FALSE----------------------------------------------------------
# Representative COMPLETE-view result, assembled offline. authkeywords is the
# single string scopus_fetch() returns under view = "COMPLETE", one element
# per record, in Scopus' own " | "-delimited form. The bibliographic fields are
# the document's own. The 'Scopus' identifier and citation count are left NA,
# since nothing on the page prints them and a guess would be a fabrication.
recs <- tibble::tibble(
  entry_number = 1L, scopus_id = NA_character_, doi = "10.1038/nature14539",
  title = "Deep learning", authors = "LeCun Y.; Bengio Y.; Hinton G.",
  year = 2015L, date = "2015-05-28", publication = "Nature",
  citations = NA_integer_,
  authkeywords =
  "deep learning | neural networks | representation learning | backpropagation"
)

## -----------------------------------------------------------------------------
recs$authkeywords

## ----eval = FALSE-------------------------------------------------------------
# ab <- scopus_abstract(
#   "10.1038/nature14539",
#   view = "FULL", include = c("references", "keywords")
# )
# ab$references[[1]][, c("title", "authors", "source", "year")]

## ----include = FALSE----------------------------------------------------------
# Representative Abstract Retrieval result: one row carrying a `references`
# list-column (a data frame of cited works, in scopus_abstract()'s schema) and
# the same n_requests / quota attributes the function attaches.
refs <- tibble::tibble(
  position = as.character(1:4),
  # The 'Scopus' identifier of each cited work is left NA, as it is whenever
  # the API does not resolve one. Inventing one for the illustration would
  # misrepresent what a real call returns.
  id = NA_character_,
  doi = c("10.1109/5.726791", "10.1038/nature14236", NA, NA),
  title = c(
    "Gradient-based learning applied to document recognition",
    "Human-level control through deep reinforcement learning",
    "ImageNet classification with deep convolutional neural networks",
    "Learning representations by back-propagating errors"
  ),
  authors = c(
    "LeCun Y.; Bottou L.; Bengio Y.; Haffner P.",
    "Mnih V.; Kavukcuoglu K.; Silver D.",
    "Krizhevsky A.; Sutskever I.; Hinton G.",
    "Rumelhart D.; Hinton G.; Williams R."
  ),
  source = c(
    "Proceedings of the IEEE", "Nature",
    "Advances in Neural Information Processing Systems", "Nature"
  ),
  year = c(1998L, 2015L, 2012L, 1986L),
  # NA is what view = "FULL" actually returns for this column. Only view =
  # "REF" populates a cited work's own citation count.
  citedbycount = NA_integer_
)
ab <- tibble::tibble(
  id = "10.1038/nature14539",
  doi = "10.1038/nature14539",
  title = "Deep learning",
  year = 2015L,
  # scopus_abstract() joins keywords "; ", like its authors column. Only
  # Search's COMPLETE view uses the " | " form shown above.
  authkeywords =
  "deep learning; neural networks; representation learning; backpropagation",
  references = list(refs)
)
attr(ab, "n_requests") <- 1L
attr(ab, "quota") <- list(remaining = 24999L, reset = NA_character_)

## -----------------------------------------------------------------------------
ab$references[[1]][, c("title", "authors", "source", "year")]

## -----------------------------------------------------------------------------
attr(ab, "n_requests")        # requests spent so far
attr(ab, "quota")$remaining   # Abstract Retrieval quota left

## ----eval = FALSE-------------------------------------------------------------
# recs <- scopus_fetch("DOI(10.1038/nature14539)", max_results = 1)
# corpus <- scopus_corpus(recs, view = "FULL")
# corpus$keywords[[1]]
# nrow(corpus$references[[1]])

## ----include = FALSE----------------------------------------------------------
# scopus_corpus() pairs each record with its Abstract Retrieval references and
# splits scopus_abstract()'s "; "-joined authkeywords string into a character
# vector per record.
corpus <- tibble::tibble(
  id = "10.1038/nature14539",
  title = "Deep learning",
  year = 2015L,
  keywords = list(trimws(strsplit(ab$authkeywords, ";", fixed = TRUE)[[1]])),
  references = list(refs)
)

## -----------------------------------------------------------------------------
corpus$keywords[[1]]
nrow(corpus$references[[1]])

## -----------------------------------------------------------------------------
sort(table(unlist(corpus$keywords)), decreasing = TRUE)

