Reads a data file using a user-supplied reader function, returning a tibble along with any error message encountered during import. This is useful when batch-reading many files where some may be malformed or inconsistent.
Usage
safe_read_data(
file,
reader,
cols = NULL,
sheet = NULL,
sheet_pattern = NULL,
anti_sheet_pattern = NULL,
...
)Arguments
- file
Character string. Path to the file to be read.
- reader
Function used to read the file (e.g.,
readr::read_csv,readxl::read_excel). The function must takefileas its first argument.- cols
Optional character vector of column names to enforce in the output. Missing columns will be added and filled with
NA, and extra columns will be dropped.- sheet
The sheet to be selected if static name. If not NULL, only the explicitly specified sheet will be read for all files.
- sheet_pattern
Optional regex pattern to select the sheet by name. If not NULL, all sheets matching the pattern will be attempted to be read.
- anti_sheet_pattern
Character or NULL. Regex pattern to exclude sheets (Excel only)
- ...
Additional arguments passed directly to
reader().
