Export Data to Various Formats (CSV/STATA/TEXT/RDS/XLSX)
Source:R/data_manipulation_export.R
export_data.Rd
Exports structured SL4 or HAR data to multiple file formats, including CSV, Stata, TXT, RDS, and XLSX. Supports nested lists, automatic subfolder creation, and multi-sheet Excel exports.
Usage
export_data(
data,
output_path,
format = "csv",
prefix = "",
create_subfolder = FALSE,
multi_sheet_xlsx = FALSE,
xlsx_filename = NULL,
report_output = FALSE
)
Arguments
- data
A list or data frame. The SL4 or HAR data to export.
- output_path
Character. The base output directory or file path.
- format
Character. The export format (
"csv"
,"stata"
,"txt"
,"rds"
,"xlsx"
). Default is"csv"
.- prefix
Character. An optional prefix added to exported file names. Default is
""
(empty).- create_subfolder
Logical. If
TRUE
, creates a subfolder for each format. Default isFALSE
.- multi_sheet_xlsx
Logical. If TRUE, exports lists as multi-sheet XLSX files.
- xlsx_filename
An optional filename for the XLSX file (used when
multi_sheet_xlsx = TRUE
).- report_output
Logical. If TRUE, generates an export report.
Details
Supports exporting data in
"csv"
,"stata"
,"txt"
,"rds"
, and"xlsx"
formats.Handles nested lists and exports each data frame individually.
Optionally creates subfolders for each format (
create_subfolder = TRUE
).Customizes file names using
prefix
.When multi_sheet_xlsx = TRUE, all exported data is stored in a single Excel workbook, with each dataset as a separate sheet.
If exporting to Stata (
"stata"
format), column names containing.
will be replaced with_
to ensure compatibility.If
multi_sheet_xlsx = TRUE
, list elements are exported as separate sheets in a single XLSX file.The function creates necessary directories if they do not exist.
Examples
if (FALSE) { # \dontrun{
# Import sample data:
sl4_data <- load_sl4x(system.file("extdata", "TAR10.sl4", package = "HARplus"))
# Extract data
data_multiple <- get_data_by_var(c("qo", "pca"), sl4_data)
# Export
export_data(data_multiple, file.path(tempdir(), "output_directory"),
format = c("csv", "xlsx", "stata", "txt", "rds"),
create_subfolder = TRUE,
multi_sheet_xlsx = TRUE)
} # }