## ----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)

## -----------------------------------------------------------------------------
plan <- scopus_plan(
  "graphene supercapacitor",
  years     = 2015:2024,
  field     = "TITLE-ABS-KEY",
  partition = "year"
)
plan

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

# A plan is a classed object, and is_scopus_plan() confirms it.
is_scopus_plan(plan)

## -----------------------------------------------------------------------------
scopus_has_key()

## ----eval = FALSE-------------------------------------------------------------
# scopus_count(
#   "graphene supercapacitor",
#   years = 2015:2024,
#   field = "TITLE-ABS-KEY"
# )
# 
# records <- scopus_fetch_plan(
#   plan,
#   cache_dir = scopus_cache_dir(),
#   resume = TRUE
# )

## -----------------------------------------------------------------------------
records <- example_records
summary(records)

head(records)

# A record set is a classed tibble, and is_scopus_records() confirms the
# contract.
is_scopus_records(records)

## -----------------------------------------------------------------------------
raw <- list(entry = list(
  list(`prism:doi` = "10.1021/am509065d",
       `dc:title` =
         "Flexible and Stackable Laser-Induced Graphene Supercapacitors",
       `dc:creator` = "Zhiwei Peng",
       `prism:publicationName` = "ACS Applied Materials & Interfaces",
       `prism:coverDate` = "2015-01-13", `citedby-count` = "469")
))
scopus_records(raw, query = "TITLE-ABS-KEY(graphene supercapacitor)")

## -----------------------------------------------------------------------------
scopus_top(records, by = "source")

## -----------------------------------------------------------------------------
dois <- scopus_extract_dois(records)
length(dois)
head(dois, 4)

## -----------------------------------------------------------------------------
baseline <- records[records$year <= 2023, ]
later <- records[-1, ]
print(scopus_diff_dois(old = baseline, new = later))

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

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

## -----------------------------------------------------------------------------
m <- as_bibliometrix(records)
head(m[, c("AU", "TI", "PY", "SO", "TC")])

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

## -----------------------------------------------------------------------------
report <- scopus_search_report(records, plan = plan)
cat(format(report, style = "paragraph"))

## ----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))
# )

