Skip to contents

Reads the option <prefix>.<option> (e.g. cb.max_docs = 10) and, when it is set, keeps only that many items (elements of a vector, rows of a data frame). Without the option, x is returned unchanged. This is the switch of test runs: options(cb.max_docs = 10) before sourcing a collector processes ten documents.

Usage

cap_items(x, prefix, option = "max_docs", sample = FALSE, seed = 1L)

Arguments

x

A vector or a data frame.

prefix

Character. The project's option prefix.

option

Character. Name of the option after the prefix.

sample

Logical. Draw the items at random (seeded) rather than keeping the first ones.

seed

Integer. Seed of the random draw.

Value

x, possibly shortened.

Details

The first items of a list are rarely typical: the oldest documents, the first source, one journal. A random draw tests the code on more varied material (sampling at random rather than by convenience is the advice of Lemercier and Zalc, 2019, ch. 2, for the same reason). The draw is seeded and does not change the caller's random numbers.

See also

Other test mode: is_test_mode()

Examples

cap_items(letters, prefix = "project")
#>  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
#> [20] "t" "u" "v" "w" "x" "y" "z"
withr::with_options(list(project.max_docs = 3), {
  print(cap_items(letters, prefix = "project"))
  cap_items(letters, prefix = "project", sample = TRUE)
})
#> ℹ `project.max_docs` is set: 3 of 26 items kept.
#> [1] "a" "b" "c"
#> ℹ `project.max_docs` is set: 3 of 26 items kept (random draw).
#> [1] "d" "g" "y"