R/utils.R

Defines functions make_file_paths load_doc_text test_nulls

Documented in load_doc_text make_file_paths test_nulls

# BSD_2_clause

#' The base file name and corresponding text file name
#'
#' @param x The (string) file path
#' @return A list with the pdf and txt file basenames
#' @export
#' @examples
#' make_file_paths("this/is/a/file.pdf")
make_file_paths <- function(x) {
  base <- basename(x)
  if(stringr::str_detect(base, "pdf$")) {
    text <- stringr::str_replace(base, "pdf$", "txt")
  } else {
    text <- paste0(base, ".txt")
    base <- paste0(base, ".pdf")
  }
  return(list(pdf = base, text = text))
}

#' Return the raw text of a file or NA if file missing
#'
#' @param path The system path to the text file of interest
#' @return A single string of the contents of the text file
#' @export
#' @examples
#' \dontrun{
#' load_doc_text("path/to/file.txt")
#' }
load_doc_text <- function(path) {
  if(!file.exists(path)) return(NA)
  return(paste(readLines(path), collapse = " "))
}

#' Test for NULL/NA/logical classes where these classes aren't expected
#'
#' @param x The object to be tested
#' @return Logical; TRUE if NULL/NA/logical, else FALSE
#' @export
#' @examples
#' \dontrun{
#' tesf <- ("path/to/file.txt")
#' }
test_nulls <- function(x) {
  if(class(x) == "NULL" |
     class(x) == "NA" |
     class(x) == "logical") {
    return(TRUE)
  }
  return(FALSE)
}
jacob-ogre/esadocs documentation built on May 18, 2019, 8 a.m.