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,
  combine = TRUE,
  file_col = "_file",
  path_col = NULL,
  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.

path_col

NULL (default) or a character scalar: name of an extra column holding the full path of the source file (absolute, "/" separators) when combine = TRUE, e.g. to extract folder names with get_path_info(). 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).

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.

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 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.

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. A column that is empty in one file takes the type it has in the others; a column whose type differs between files (e.g. a date in one, text in another) is combined as text, with a warning naming it (see import_xlsx, Details).

  • 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

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
)
#>                                                           _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