Skip to content

Save RDS with Metadata

The save_rds_with_metadata() function in the reportifyr package is an extension of the base saveRDS() function. It saves R objects as both an .RDS file and as an .RTF file via reportifyr’s save_as_rtf() function, while also simultaneously capturing analysis-relevant metadata in a .json file via reportifyr’s write_object_metadata() function.


save_rds_with_metadata()

R: Wrapper around the saveRDS function. Saves an object as .RDS...
save_rds_with_metadataR Documentation

Description

Extension to the saveRDS() function that allows capturing object metadata as a separate .json file.

Usage

save_rds_with_metadata(
  object,
  file = "",
  meta_type = "NA",
  meta_equations = NULL,
  meta_notes = NULL,
  meta_abbrevs = NULL,
  table1_format = FALSE,
  ...
)

Arguments

object

The R object to serialize.

file

The connection or name of the file where the R object is saved.

meta_type

A string to specify the type of object. Default is "NA".

meta_equations

A string or vector of strings representing equations to include in the metadata. Default is NULL.

meta_notes

A string or vector of strings representing notes to include in the metadata. Default is NULL.

meta_abbrevs

A string or vector of strings representing abbreviations to include in the metadata. Default is NULL.

table1_format

A boolean indicating whether to apply table1-style formatting. Default is FALSE.

...

Additional arguments passed to the saveRDS() function.

Examples

## Not run: 

# ---------------------------------------------------------------------------
# Save a simple table
# ---------------------------------------------------------------------------
tables_path <- here::here("OUTPUTS", "tables")
outfile_name <- "01-12345-pk-theoph.RDS"

save_rds_with_metadata(
  object = Theoph,
  file = file.path(tables_path, outfile_name)
)

## End(Not run)

Returns

  • RDS File: The serialized R object saved to the specified file.
  • RTF File: An .RTF file of the serialized R object, formatted as table1 if specified.
  • Metadata File: A .json file saved with metadata.

If the source file is not tracked by Git, a similar warning is displayed:

2025-01-10 18:30:00 [WARN] Source file path not tracked by git: /cluster-data/user-homes/user/project/scripts/analysis.R

Logging

  • Debug: Logs the start and end of the function, as well as metadata validation and writing.
  • Info: Logs a confirmation of both .RDS and .RTF file creation, as well as metadata capture.

Additional Resources

  • reportifyr GitHub: Refer to the reportifyr repository for more details.
  • saveRDS Documentation: Refer to the saveRDS() function for additional arguments to pass to save_rds_with_metadata().
  • Write Object Metadata Reference: Refer to the reference on write_object_metadata() here.