cpt_export: Write a result to a file, and read it back

View source: R/json.R

cpt_exportR Documentation

Write a result to a file, and read it back

Description

cpt_export() writes a result where software in any language can read it; cpt_import() rebuilds a working ggcpt from the file. A saved .rds already reads in any R session without this package (a ggcpt is a plain list of plain data), so these are for leaving R, or for a format a person can open.

Usage

cpt_export(fit, file, format = NULL, ...)

cpt_import(file, format = NULL, method = "imported", change_in = "mean")

Arguments

fit

A ggcpt object.

file

Path to write (cpt_export()) or read (cpt_import()).

format

"json" (the full result, see as_json()) or "csv" (one row per observation: index, value (for a multivariate result, one value_<name> per coordinate), the time index as index_value when there is one, seg_id, fitted and is_changepoint, which is everything needed to rebuild the segmentation but not its metadata). Defaults to the file's extension.

...

For cpt_export(), further arguments for as_json().

method, change_in

For a CSV, which carries no metadata, the method and change type to record on the rebuilt result. Defaults to "imported" and "mean".

Value

cpt_export() returns file, invisibly. cpt_import() returns a ggcpt object; the engine's own $fit is not stored in either format, so it is NULL.

See Also

Other result class: annotate_segments(), as_cpt_series(), as_ggcpt(), as_json(), cpt_annotations(), cpt_verify(), ggchangepoint-conditions, is_ggcpt(), new_ggcpt(), print.ggcpt()

Examples


set.seed(1)
fit <- cpt_detect(c(rnorm(50), rnorm(50, 3)), method = "pelt")
path <- tempfile(fileext = ".json")
cpt_export(fit, path)
back <- cpt_import(path)
back$changepoints
unlink(path)


ggchangepoint documentation built on Nov. 5, 2026, 9:06 a.m.