Combines latest_file() with a file reader to load the most recent
file matching a pattern. Automatically detects file type and uses
the appropriate reader.
Arguments
- path
Directory path to search in.
- pattern
Regular expression pattern to filter files. Default NULL matches all files.
- recursive
Logical. If TRUE, searches subdirectories. Default FALSE.
- reader
Function to read the file. Default NULL auto-detects based on file extension (supports .csv, .rds, .rda).
- ...
Additional arguments passed to the reader function.
Details
Auto-detection supports these file types:
.csv: Uses
utils::read.csv().rds: Uses
readRDS().rda/.RData: Uses
load()and returns the first object
For other file types or custom reading behavior, provide a reader
function.
Examples
if (FALSE) { # \dontrun{
# Read the most recent CSV
df <- read_latest("data/exports", pattern = "\\.csv$")
# Read with custom options
df <- read_latest("data/exports", pattern = "\\.csv$",
stringsAsFactors = FALSE)
# Use a custom reader
df <- read_latest("data/exports", pattern = "\\.xlsx$",
reader = readxl::read_excel)
} # }