inst/doc/file-formats.R

## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
has <- function(p) requireNamespace(p, quietly = TRUE)

## -----------------------------------------------------------------------------
library(SynthesizerPlus)

## -----------------------------------------------------------------------------
supported_formats()

## -----------------------------------------------------------------------------
dir <- tempfile("sp-formats-")
dir.create(dir)
syn <- synthesize(iris, n = 300, seed = 1)

write_data(syn, file.path(dir, "iris.csv"))
write_data(syn, file.path(dir, "iris.tsv.gz"))
write_data(syn, file.path(dir, "iris.rds"))
list.files(dir)

## -----------------------------------------------------------------------------
x <- read_data(file.path(dir, "iris.csv"))
class(x$Species)
x <- read_data(file.path(dir, "iris.csv"), template = iris)
levels(x$Species)

## ----eval = has("arrow") || has("nanoparquet")--------------------------------
f <- file.path(dir, "iris.parquet")
write_data(syn, f)
str(read_data(f))

## ----eval = has("hdf5r")------------------------------------------------------
f <- file.path(dir, "iris.h5")
write_data(syn, f)
identical(read_data(f), syn)

## ----eval = has("jsonlite")---------------------------------------------------
write_data(head(syn, 2), file.path(dir, "iris.json"))
cat(readLines(file.path(dir, "iris.json")), sep = "\n")

## ----eval = has("haven")------------------------------------------------------
write_data(syn, file.path(dir, "iris.sav"))   # SPSS
str(read_data(file.path(dir, "iris.sav")))
write_data(syn, file.path(dir, "iris.dta"))   # Stata: names are made valid
names(read_data(file.path(dir, "iris.dta")))

## -----------------------------------------------------------------------------
real <- read_data(file.path(dir, "iris.rds"))
synthesize(real, n = 1000, seed = 1, file = file.path(dir, "synthetic.csv.gz"))
nrow(read_data(file.path(dir, "synthetic.csv.gz")))

## -----------------------------------------------------------------------------
fit <- fit_synthesizer(iris)
res <- generate_to_file(fit, n = 50000, path = file.path(dir, "big.csv"),
                        chunk_size = 10000, seed = 1)
nrow(read_data(file.path(dir, "big.csv")))

res <- generate_to_file(fit, n = 50000, path = file.path(dir, "big_rds"),
                        format = "rds", chunk_size = 20000, seed = 1)
basename(res$files)
nrow(read_data(file.path(dir, "big_rds")))

## ----eval = FALSE-------------------------------------------------------------
# # e.g. 100 million rows as a partitioned Parquet data set
# generate_to_file(fit, n = 1e8, path = "synthetic_parquet", format = "parquet",
#                  chunk_size = 1e6, seed = 1)
# ds <- arrow::open_dataset("synthetic_parquet")

## ----include = FALSE----------------------------------------------------------
unlink(dir, recursive = TRUE)

Try the SynthesizerPlus package in your browser

Any scripts or data that you put into this service are public.

SynthesizerPlus documentation built on Oct. 16, 2026, 5:10 p.m.