list_files: List files in a directory

Description Usage Arguments Value Examples

View source: R/list_files.R

Description

Wrapper to base::list.files with additional options for ignoring temporary files (which on some systems begin with "~$"), and for selecting among multiple files based on a date- or time-stamp extracted from the filename.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
list_files(
  path = ".",
  pattern = NULL,
  full.names = TRUE,
  recursive = FALSE,
  ignore.case = TRUE,
  include.dirs = FALSE,
  ignore_temp = TRUE,
  select = c("all", "latest"),
  warn_no_date = TRUE
)

Arguments

path

a character vector of full path names; the default corresponds to the working directory, getwd(). Tilde expansion (see path.expand) is performed. Missing values will be ignored. Elements with a marked encoding will be converted to the native encoding (and if that fails, considered non-existent).

pattern

an optional regular expression. Only file names which match the regular expression will be returned.

full.names

a logical value. If TRUE, the directory path is prepended to the file names to give a relative file path. If FALSE, the file names (rather than paths) are returned.

recursive

logical. Should the listing recurse into directories?

ignore.case

logical. Should pattern-matching be case-insensitive?

include.dirs

logical. Should subdirectory names be included in recursive listings? (They always are in non-recursive ones).

ignore_temp

Logical indicating whether to ignore temporary files (files starting with "~$"). Defaults to TRUE.

select

Select all matching files ("all") or only the single most recent file ("latest") as determined by a date or date-time stamp extracted from the file name using functions extract_date and extract_datetime, respectively.

When select is "latest" and there is:

  • a single matching file name, the single match is returned regardless of whether it contains a date/datetime

  • multiple matching file names none of which contain a date/datetime, an error is thrown

  • multiple matching file names, where some contain a date/datetime and some do not, a warning is given if argument warn_no_date is TRUE and the names lacking a date/datetime are excluded

  • multiple matching file names where there is a tie for latest date/datetime, an error is thrown

warn_no_date

Logical indicating whether to give warning

Value

A character vector containing the names of the files in the specified directories, that match the given set of arguments (an empty character vector if no matching files are found)

Examples

1
2
3
4
## Not run: 
list_files("~/Documents", pattern = "^exported_data_", select = "latest")

## End(Not run)

epicentre-msf/llutils documentation built on Nov. 9, 2020, 8:24 p.m.