import_xlsx {mintyr} | R Documentation |
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, ...)
Arguments
file |
A |
rbind |
A
|
sheet |
A
|
... |
Additional arguments passed to |
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
Value
Depends on the rbind
parameter:
If
rbind = TRUE
: A singledata.table
with additional tracking columns: -excel_name
: Source file name (without extension) -sheet_name
: Source sheet nameIf
rbind = FALSE
: A named list ofdata.table
s with format"filename_sheetname"
Note
Critical Import Considerations:
Requires all specified files to be accessible
Excel
filesSheet indices must be valid across input files
-
rbind = TRUE
assumes compatible data structures Missing columns are automatically filled with
NA
File extensions are automatically removed in tracking columns
See Also
-
readxl::read_excel()
for underlying Excel reading -
data.table::rbindlist()
for data combination
Examples
# 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
)