Learn R Programming

mintyr (version 0.1.0)

import_xlsx: Import Data from XLSX Files with Advanced Handling

Description

A robust and flexible function for importing data from one or multiple XLSX files, offering comprehensive options for sheet selection, data combination, and source tracking.

Usage

import_xlsx(file, rbind = TRUE, sheet = NULL, ...)

Value

Depends on the rbind parameter:

  • If rbind = TRUE: A single data.table with additional tracking columns: - excel_name: Source file name (without extension) - sheet_name: Source sheet name

  • If rbind = FALSE: A named list of data.tables with format "filename_sheetname"

Arguments

file

A character vector of file paths to Excel files. Must point to existing .xlsx or .xls files.

rbind

A logical value controlling data combination strategy:

  • TRUE: Combines all data into a single data.table

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

sheet

A numeric vector or NULL specifying sheet import strategy:

  • NULL (default): Imports all sheets

  • numeric: Imports only specified sheet indices

...

Additional arguments passed to readxl::read_excel(), such as col_types, skip, or na.

Details

The function provides a comprehensive solution for importing Excel data with the following features:

  • Supports multiple files and sheets

  • Automatic source tracking for files and sheets

  • Flexible combining options

  • Handles missing columns across sheets when combining

  • Preserves original data types through readxl

See Also

Examples

Run this code
# Example: Excel file import demonstrations

# Setup test files
xlsx_files <- mintyr_example(
  mintyr_examples("xlsx_test")    # Get example Excel files
)

# Example 1: Import and combine all sheets from all files
import_xlsx(
  xlsx_files,                     # Input Excel file paths
  rbind = TRUE                    # Combine all sheets into one data.table
)

# Example 2: Import specific sheets separately
import_xlsx(
  xlsx_files,                     # Input Excel file paths
  rbind = FALSE,                  # Keep sheets as separate data.tables
  sheet = 2                       # Only import first sheet
)

Run the code above in your browser using DataLab