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.

import_csv(
  file,
  rbind = TRUE,
  rbind_label = "_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.

rbind

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.

rbind_label

A character scalar or NULL specifying the source-tracking column name (default: "_file"). Set to NULL to suppress the source column. Only applies when rbind = 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).

Value

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

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

Details

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

full_path = FALSE, keep_ext = FALSEFilename without extension: "data"
full_path = FALSE, keep_ext = TRUEFilename with extension: "data.csv"
full_path = TRUE, keep_ext = FALSEFull path without extension: "/path/to/data"
full_path = TRUE, keep_ext = TRUEFull path with extension: "/path/to/data.csv"

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

Note

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

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

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

See also

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
  rbind = TRUE,                   # Combine all files into one data.table
  rbind_label = "_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
)
#>                                                           _file  col1   col2
#>                                                          <char> <int> <char>
#> 1: /home/runner/work/_temp/Library/mintyr/extdata/csv_test1.csv     4      d
#> 2: /home/runner/work/_temp/Library/mintyr/extdata/csv_test1.csv     5      f
#> 3: /home/runner/work/_temp/Library/mintyr/extdata/csv_test1.csv     6      e
#> 4: /home/runner/work/_temp/Library/mintyr/extdata/csv_test2.csv    15      o
#> 5: /home/runner/work/_temp/Library/mintyr/extdata/csv_test2.csv    16      p
#> 6: /home/runner/work/_temp/Library/mintyr/extdata/csv_test2.csv    17      q
#>      col3
#>    <lgcl>
#> 1:  FALSE
#> 2:   TRUE
#> 3:   TRUE
#> 4:  FALSE
#> 5:   TRUE
#> 6:  FALSE