| export_nest | R Documentation |
Exports list-columns containing data.frame or data.table objects from a
data.frame/data.table to txt or csv files, automatically
constructing a hierarchical directory structure from non-nested columns.
Exportable nested columns (those holding data.frame/data.table elements)
are distinguished from non-exportable custom-object columns (e.g. fitted model
objects); only the former are written to disk by default.
export_nest(
data,
by = NULL,
cols = NULL,
path = tempdir(),
file_type = "txt",
na = "NA",
quote = FALSE,
...
)
data |
A |
by |
Optional character vector of column names used to build the
hierarchical output directory structure. When |
cols |
Optional character vector of nested column names to export. When
|
path |
Single character string specifying the root export directory.
Defaults to |
file_type |
Either |
na |
Single string written for missing values. Default |
quote |
Passed to |
... |
Further arguments passed to |
Nested column classification (mutually exclusive):
Exportable — every element inherits from data.frame or data.table.
Non-exportable — empty lists or elements of any other class (e.g. model objects). Reported to the console; never written.
Directory layout:
path / <group1_value> / <group2_value> / <nest_col_name>.<file_type>
Group values are sanitised (characters that are invalid in file names, such as slashes, colons or asterisks, become underscores), so they can never create extra directory levels. The grouping columns must identify every row uniquely; otherwise rows would overwrite each other's files and the function stops with an error listing the duplicated targets.
Performance notes:
Row data is accessed via .subset2() (zero-copy column access) rather than
data[i], eliminating per-row data.table allocation in the hot loop.
All output directory paths are pre-computed in a single vectorised
do.call(file.path, ...) call; only the unique directories are created.
The field separator and output filenames are computed once before the loop.
An invisible character vector of the files written
(length 0 when nothing was written).
fwrite
# Example: Basic nested data export workflow
# A dedicated sub-folder of tempdir() keeps the clean-up safe
out_dir <- file.path(tempdir(), "mintyr_export_nest")
# Step 1: Create nested data structure
dt_nest <- w2l_nest(
data = iris, # Input iris dataset
cols = 1:2, # Columns to be nested
by = "Species" # Grouping variable
)
# Step 2: Export nested data to files
files <- export_nest(
data = dt_nest, # Input nested data.table
cols = "data", # Column containing nested data
by = c("name", "Species"), # Columns to create directory structure
path = out_dir
)
# Returns (invisibly) the paths of the written files
# Directory structure: out_dir/<name>/<Species>/data.txt
files
# Clean up
unlink(out_dir, recursive = TRUE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.