Skip to contents

This function is a simple helper to calculate the total number of pages of an article.

Usage

jst_get_total_pages(first_page, last_page, page_range, quietly = FALSE)

Arguments

first_page

The first page of an article (numeric).

last_page

The last page of an article (numeric).

page_range

The page range of an article (character).

quietly

Sometimes page ranges contain roman numerals like xiv. These are not recognized, return NA and raise a warning. If set to TRUE, this warning not raised.

Value

A vector with the calculated total pages.

Details

This function deals with four cases:

  • if all three arguments are missing, NA is returned.

  • if page_range is supplied, the number of pages is calculated from it.

  • if only the first page is supplied, NA is returned.

  • if first and last page are supplied, the number of pages is calculated as last_page - first_page + 1.

The algorithm to parse page ranges works as follows: A typical page range is 1-10, 200 where the article starts at page 1, ends at page 10, and has an erratum at page 200. For this case, the range is calculated as range + single_page, as in(10 - 1 + 1) + 1 = 11. Sometimes multiple ranges are given: 1-10, 11-20. For those cases all ranges are summed: (10 - 1 + 1) + (20 - 11 + 1) = 20. Another specification for multiple ranges is 1-10+11-20, which is treated similarly.

Examples

# calculate pages from first and last page
first_pages <- sample(30:50, 10)
last_pages <- first_pages + sample(5:20, 10)
page_ranges <- rep(NA_character_, 10)

jst_get_total_pages(first_pages, last_pages, page_ranges)
#>  [1] 10 21 13 18  7 12 20 15  9 14

# get pages from page range
jst_get_total_pages(NA_real_, NA_real_, "51 - 70")
#> [1] 20
jst_get_total_pages(NA_real_, NA_real_, "51 - 70, 350")
#> [1] 21
jst_get_total_pages(NA_real_, NA_real_, "350, 51 - 70")
#> [1] 21
jst_get_total_pages(NA_real_, NA_real_, "51 - 70, 80-100")
#> [1] 41
jst_get_total_pages(NA_real_, NA_real_, "51-70+350")
#> [1] 21