R/count_trees_in_file.R

Defines functions count_trees_in_file

Documented in count_trees_in_file

#' Count the number of trees in a \code{.trees} file
#' @inheritParams default_params_doc
#' @return the number of trees
#' @seealso if the \code{.trees} file is invalid,
#'   use \link{is_trees_file} with \code{verbose = TRUE}
#'   for the reason
#' @author Richèl J.C. Bilderbeek
#' @export
count_trees_in_file <- function(
  trees_filename
) {
  if (!file.exists(trees_filename)) {
    stop(
      "'trees_filename' absent.\n",
      "File '", trees_filename, "' not found"
    )
  }
  if (!tracerer::is_trees_file(trees_filename = trees_filename)) {
    stop(
      "'trees_filename' invalid"
    )
  }
  # pattern = "(^tree STATE_)|(\tTREE \\* UNTITLED = \\[&R\\] \\()")
  sum(
    stringr::str_detect(
      string = readLines(trees_filename, warn = FALSE),
      pattern = "(^tree STATE_)|(\tTREE \\* (UNTITLED|STATE_[:digit:]*) = \\[&R\\] \\()"
    )
  )
}

Try the tracerer package in your browser

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

tracerer documentation built on Sept. 27, 2023, 5:09 p.m.