import_csv: Flexible 'CSV'/'TXT' File Import with Multiple Backend...

import_csvR Documentation

Flexible CSV/TXT File Import with Multiple Backend Support

Description

A comprehensive CSV or TXT file import function offering advanced reading capabilities through data.table and arrow packages with intelligent data combination strategies.

Usage

import_csv(
  file,
  package = "data.table",
  rbind = TRUE,
  rbind_label = "_file",
  ...
)

Arguments

file

A character vector of file paths to CSV files. Must point to existing and accessible files.

package

A character string specifying the backend package:

  • "data.table": Uses data.table::fread() (default)

  • "arrow": Uses arrow::read_csv_arrow() Determines the underlying reading mechanism.

rbind

A logical value controlling data combination strategy:

  • TRUE: Combines all files into a single data object

  • FALSE: Returns a list of individual data objects Default is TRUE.

rbind_label

A character string or NULL for source file tracking:

  • character: Specifies the column name for file source labeling

  • NULL: Disables source file tracking Default is "_file".

...

Additional arguments passed to backend-specific reading functions (e.g., col_types, na.strings, skip).

Details

The function provides a unified interface for reading CSV files using either data.table or arrow package. When reading multiple files, it can either combine them into a single data object or return them as a list. File source tracking is supported through the rbind_label parameter.

Value

Depends on the rbind parameter:

  • If rbind = TRUE: A single data object (from chosen package) containing all imported data

  • If rbind = FALSE: A named list of data objects with names derived from input file names (without extensions)

Note

Critical Import Considerations:

  • Requires all specified files to be accessible CSV/TXT files

  • Supports flexible backend selection

  • rbind = TRUE assumes compatible data structures

  • Missing columns are automatically aligned

  • File extensions are automatically removed in tracking columns

See Also

  • data.table::fread() for data.table backend

  • arrow::read_csv_arrow() for arrow backend

  • data.table::rbindlist() for data combination

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
  package = "data.table",         # Use data.table for reading
  rbind = TRUE,                   # Combine all files into one data.table
  rbind_label = "_file"           # Column name for file source
)

# Example 2: Import files separately using arrow
import_csv(
  csv_files,                      # Input CSV file paths
  package = "arrow",              # Use arrow for reading
  rbind = FALSE                   # Keep files as separate data.tables
)

mintyr documentation built on April 4, 2025, 2:56 a.m.