R/filenames.R

Defines functions filenames

Documented in filenames

#' Filenames from paths
#'
#' This function takes a character vector of paths and returns just the file
#' name, by default without the extension. A \code{\link{TextReuseCorpus}} uses
#' the paths to the files in the corpus as the names of the list. This function
#' is intended to turn those paths into more manageable identifiers.
#'
#' @param paths A character vector of paths.
#' @param extension Should the file extension be preserved?
#' @seealso \code{\link{basename}}
#' @examples
#' paths <- c("corpus/one.txt", "corpus/two.md", "corpus/three.text")
#' filenames(paths)
#' filenames(paths, extension = TRUE)
#' @export
filenames <- function(paths, extension = FALSE) {
  assert_that(is.character(paths))
  f <- basename(paths)
  if (extension)
    return(f)
  else
    str_replace(f, "\\.[:alpha:]{1,}$", "")
}

Try the textreuse package in your browser

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

textreuse documentation built on July 8, 2020, 6:40 p.m.