Skip to content

Write CSV with Metadata

The write_csv_with_metadata() function in the reportifyr package is an extension of the write.csv() function from the utils package. It saves data objects as both .csv files 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.


write_csv_with_metadata()

R: Wrapper around the write.csv function. Saves data as .RDS and...
write_csv_with_metadataR Documentation

Description

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

Usage

write_csv_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 utils::write.csv() function.

Examples

## Not run: 

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

write_csv_with_metadata(
  object = Theoph,
  file = file.path(tables_path, out_name),
  row_names = FALSE
)

## End(Not run)

Returns

  • csv File: The data 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 .csv and .RTF file creation, as well as metadata capture.

Additional Resources

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