import_csv: Flexible CSV/TXT File Import via data.table

View source: R/import_csv.R

import_csvR Documentation

Flexible CSV/TXT File Import via data.table

Description

Reads one or more CSV/TXT files using fread as the backend. Supports flexible combination strategies and source-file tracking. All return values are data.table objects.

Usage

import_csv(
  file,
  combine = TRUE,
  file_col = "_file",
  full_path = FALSE,
  keep_ext = FALSE,
  ...
)

Arguments

file

A non-empty character vector of file paths to CSV/TXT files. All paths must point to existing, accessible files.

combine

A logical scalar controlling the combination strategy:

  • TRUE (default): Combine all files into a single data.table.

  • FALSE: Return a named list of individual data.table objects.

file_col

A character scalar or NULL specifying the source-tracking column name (default: "_file"). Set to NULL to suppress the source column. Only applies when combine = TRUE.

full_path

A logical scalar controlling path representation in labels:

  • FALSE (default): Use only the filename (via basename()).

  • TRUE: Use the full file path.

keep_ext

A logical scalar controlling whether the file extension is retained in labels:

  • FALSE (default): Strip the file extension (e.g., "data").

  • TRUE: Retain the file extension (e.g., "data.csv").

...

Additional arguments passed directly to fread (e.g., select, drop, na.strings, skip, nThread).

Details

Label generation is controlled by the combination of full_path and keep_ext:

full_path = FALSE, keep_ext = FALSE Filename without extension: "data"
full_path = FALSE, keep_ext = TRUE Filename with extension: "data.csv"
full_path = TRUE, keep_ext = FALSE Full path without extension: "/path/to/data"
full_path = TRUE, keep_ext = TRUE Full path with extension: "/path/to/data.csv"

When combine = TRUE and file_col is not NULL, rbindlist is called with idcol = file_col, which generates the source column directly during the merge step without any intermediate copies.

Value

  • combine = TRUE: A single data.table containing all imported rows. If file_col is not NULL, the first column contains the source file label for each row.

  • combine = FALSE: A named list of data.table objects. List names are derived from file paths according to full_path and keep_ext settings.

Note

  • All specified files must exist and be readable at call time.

  • combine = TRUE assumes compatible column structures across files; mismatched columns are automatically aligned via fill = TRUE.

  • Logical parameters (combine, full_path, keep_ext) reject NA values explicitly.

  • Only the extension of the file itself is stripped; dots in directory names (e.g. "v1.2/file") are left intact.

  • Duplicated labels (same file name in different folders) trigger a warning and are made unique with make.unique().

See Also

fread, rbindlist

Examples

# Example: CSV file import demonstrations

# Setup test files
csv_files <- mintyr_example(
  mintyr_examples("csv_test")     # Get example CSV files
)

# Example 1: Import and combine CSV files using data.table
import_csv(
  csv_files,                      # Input CSV file paths
  combine = TRUE,                 # Combine all files into one data.table
  file_col = "_file",             # Column name for file source
  keep_ext = TRUE,                # Include .csv extension in _file column
  full_path = TRUE                # Show complete file paths in _file column
)

mintyr documentation built on Oct. 5, 2026, 5:08 p.m.