R/parse_pages_to_remove.R

Defines functions parse_pages_to_remove

Documented in parse_pages_to_remove

#' Parse Pages of a PDF
#'
#' @param input_string String containing pages to remove
#'
#' @return a numeric vector containing pages to remove
#' @keywords internal
parse_pages_to_remove <- function(input_string) {
  parts <- unlist(strsplit(input_string, ",")) # Split by commas
  pages <- unlist(lapply(parts, function(part) {
    if (grepl("-", part)) { # Handle ranges (e.g., "5-10")
      range <- suppressWarnings(as.numeric(unlist(strsplit(part, "-"))))
      if (length(range) == 2 && all(!is.na(range)) && range[1] <= range[2]) seq(range[1], range[2])
    } else { # Handle single page numbers
      if (!is.na(suppressWarnings(as.numeric(part)))) as.numeric(part)
    }
  }))
  if (is.null(pages)) {
    return(numeric(0)) # Explicitly return an empty numeric vector if no valid pages are found
  }
  unique(pages[!is.na(pages)])
}

Try the pdfcombiner package in your browser

Any scripts or data that you put into this service are public.

pdfcombiner documentation built on Sept. 9, 2025, 5:56 p.m.