## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")

## ----setup--------------------------------------------------------------------
library(scopusflow)

## -----------------------------------------------------------------------------
plan <- scopus_plan(
  "machine translation",
  years     = 2018:2020,
  field     = "TITLE-ABS-KEY",
  partition = "year"
)
plan

## -----------------------------------------------------------------------------
scopus_plan("language learning", field = "TITLE")$query
scopus_plan("x", years = 2015:2020)$date

## ----eval = FALSE-------------------------------------------------------------
# scopus_count("machine translation", years = 2018:2020, field = "TITLE-ABS-KEY")
# 
# records <- scopus_fetch_plan(plan, cache_dir = scopus_cache_dir(), resume = TRUE)

## -----------------------------------------------------------------------------
records <- example_records
records

## -----------------------------------------------------------------------------
dois <- scopus_extract_dois(records)
dois

# Suppose a later retrieval added one DOI and dropped another.
later <- c(dois[-1], "10.1000/example.999")
scopus_diff_dois(old = dois, new = later)

## -----------------------------------------------------------------------------
out <- file.path(tempdir(), "dois.csv")
scopus_extract_dois(records, file = out)
readLines(out)

## ----eval = FALSE-------------------------------------------------------------
# cmp <- scopus_compare_topics(
#   reference_query  = "language learning",
#   comparison_terms = c("effect size", "Bayesian"),
#   years            = 2015:2020,
#   field            = "TITLE-ABS-KEY"
# )

## -----------------------------------------------------------------------------
# A stand-in comparison object with the same columns scopus_compare_topics()
# returns, so the plotting step is reproducible offline.
cmp <- tibble::tibble(
  query = "q",
  query_type = rep(c("reference", "comparison", "comparison"), each = 6),
  abridged_query = rep(c("language learning", "effect size", "Bayesian"), each = 6),
  year = rep(2015:2020, 3),
  n = c(rep(100, 6), 20, 24, 30, 33, 40, 45, 5, 7, 9, 12, 15, 19),
  reference_n = rep(100, 18),
  comparison_percentage = c(rep(100, 6), 20, 24, 30, 33, 40, 45, 5, 7, 9, 12, 15, 19),
  average_comparison_percentage = rep(c(100, 32, 11.2), each = 6)
)
class(cmp) <- c("scopus_comparison", class(cmp))
cmp

## ----fig.alt = "Line chart of two topics' share of the reference literature over time", fig.width = 7, fig.height = 4.5----
if (requireNamespace("ggplot2", quietly = TRUE)) {
  plot_scopus_comparison(cmp)
}

## -----------------------------------------------------------------------------
head(as_bibliometrix(records))

path <- file.path(tempdir(), "records.rds")
write_scopus_records(records, path)
identical(read_scopus_records(path), records)

## ----eval = FALSE-------------------------------------------------------------
# tryCatch(
#   scopus_fetch("..."),
#   scopus_error_no_key     = function(e) message("No API key configured."),
#   scopus_error_rate_limit = function(e) message("Rate limited, so backing off."),
#   scopus_error            = function(e) message("Scopus error: ", conditionMessage(e))
# )

