R/export_list.R
export_list.RdExports every element of a named (or unnamed) list of data.frame /
data.table objects to txt or csv files. Element names may
contain forward-slashes (/) to encode arbitrary subdirectory depth, e.g.
"group_a/subject_01/results" writes
<export_path>/group_a/subject_01/results.txt.
Unnamed elements are automatically labelled split_<i>.
export_list(split_dt, export_path = tempdir(), file_type = "txt")A non-empty list whose elements are data.frame,
data.table, or any object coercible via data.table::as.data.table().
Single character string - the root export directory.
Created recursively if absent. Defaults to tempdir().
"txt" (tab-separated, default) or "csv"
(comma-separated). Case-insensitive.
An invisible named character vector of the absolute file paths
written, with length equal to the number of successfully exported elements.
The total count is accessible via length() on the return value.
Performance design:
All element names are resolved and path components split in a single vectorised pass before the write loop, so no string work occurs inside the hot path.
Unique subdirectories are collected and created in one batch
(k dir.create() syscalls, where k \(\le\) n).
The field separator is resolved once at function entry.
as.data.table() on an existing data.table is a
reference-pass (no copy).
Error handling:
Individual element failures emit a warning and are skipped; the
remaining elements continue to be processed.
Requires the data.table package.
# Example: Export split data to files
# Step 1: Create split data structure
dt_split <- w2l_split(
data = iris, # Input iris dataset
cols2l = 1:2, # Columns to be split
by = "Species" # Grouping variable
)
# Step 2: Export split data to files
export_list(
split_dt = dt_split # Input list of data.tables
)
#> [ export_list ] Export complete. 6 / 6 file(s) written to: /tmp/RtmpMDr3j5
# Returns the number of files created
# Files are saved in tempdir() with .txt extension
# Check exported files
list.files(
path = tempdir(), # Default export directory
pattern = "txt", # File type pattern to search
recursive = TRUE # Search in subdirectories
)
#> [1] "Sepal.Length_setosa.txt" "Sepal.Length_versicolor.txt"
#> [3] "Sepal.Length_virginica.txt" "Sepal.Width_setosa.txt"
#> [5] "Sepal.Width_versicolor.txt" "Sepal.Width_virginica.txt"
# Clean up exported files
files <- list.files(
path = tempdir(), # Default export directory
pattern = "txt", # File type pattern to search
recursive = TRUE, # Search in subdirectories
full.names = TRUE # Return full file paths
)
file.remove(files) # Remove all exported files
#> [1] TRUE TRUE TRUE TRUE TRUE TRUE