This function saves an eim
object to a specified file format. Supported formats are
RDS, JSON, and CSV. The function dynamically extracts and saves all available
attributes when exporting to JSON. If the prob
field exists, it is saved when using CSV;
otherwise, it yields an error.
save_eim(object, filename, ...)
The function does not return anything explicitly but saves the object to the specified file.
If the file extension is RDS, the entire object is saved using saveRDS()
.
If the file extension is JSON, all available attributes of the object are stored in JSON format.
If the file extension is CSV:
If the object contains a prob
field, only that field is saved as a CSV.
Otherwise, returns an error.
The eim object implementation.
# \donttest{
model <- eim(X = matrix(1:9, 3, 3), W = matrix(1:9, 3, 3))
model <- run_em(model)
td <- tempdir()
out_rds <- file.path(td, "model_results.rds")
out_json <- file.path(td, "model_results.json")
out_csv <- file.path(td, "model_results.csv")
# Save as RDS
save_eim(model, filename = out_rds)
#> Results saved as RDS: /var/folders/kg/_zd3ll1x7zz9vkxv49rq_m9m0000gn/T//Rtmp50te7h/model_results.rds
# Save as JSON
save_eim(model, filename = out_json)
#> Results saved as JSON: /var/folders/kg/_zd3ll1x7zz9vkxv49rq_m9m0000gn/T//Rtmp50te7h/model_results.json
# Save as CSV
save_eim(model, filename = out_csv)
#> Probability matrix saved as CSV: /var/folders/kg/_zd3ll1x7zz9vkxv49rq_m9m0000gn/T//Rtmp50te7h/model_results.csv
# Remove the files
files <- c(out_rds, out_json, out_csv)
file.remove(files)
#> [1] TRUE TRUE TRUE
# }