Title: | Import and Export CSV Data with a YAML Metadata Header |
---|---|
Description: | Support for import from and export to the CSVY file format. CSVY is a file format that combines the simplicity of CSV (comma-separated values) with the metadata of other plain text and binary formats (JSON, XML, Stata, etc.) by placing a YAML header on top of a regular CSV. |
Authors: | Thomas J. Leeper [aut, cre] |
Maintainer: | Thomas J. Leeper <[email protected]> |
License: | GPL-2 |
Version: | 0.3.0 |
Built: | 2025-01-25 05:34:43 UTC |
Source: | https://github.com/leeper/csvy |
Dictionary of column classes for reading data
colclass_dict
colclass_dict
An object of class character
of length 7.
CSVY is a file format that combines the simplicity of CSV (comma-separated values) with the metadata of other plain text and binary formats (JSON, XML, Stata, etc.). The CSVY file specification is simple: place a YAML header on top of a regular CSV. The csvy package implements this format using two functions: write_csvy
and read_csvy
.
Note that this assumes only one Yaml header, starting on the first line of the file.
get_yaml_header(file, yaml_rxp = "^\\#*---[[:space:]]*$", verbose = TRUE)
get_yaml_header(file, yaml_rxp = "^\\#*---[[:space:]]*$", verbose = TRUE)
file |
A character string or R connection specifying a file. |
yaml_rxp |
Regular expression for parsing YAML header |
verbose |
Logical. If |
Character vector of lines containing YAML header, or 'NULL' if no YAML header found.
Import CSVY data as a data.frame
read_csvy(file, metadata = NULL, stringsAsFactors = FALSE, detect_metadata = TRUE, ...)
read_csvy(file, metadata = NULL, stringsAsFactors = FALSE, detect_metadata = TRUE, ...)
file |
A character string or R connection specifying a file. |
metadata |
Optionally, a character string specifying a YAML (“.yaml”) or JSON (“.json”) file containing metadata (in lieu of including it in the header of the file). |
stringsAsFactors |
A logical specifying whether to treat character columns as factors. Passed to |
detect_metadata |
A logical specifying whether to auto-detect a metadata file if none is specified (and if no header is found). |
... |
Additional arguments passed to |
read_csvy(system.file("examples", "example1.csvy", package = "csvy"))
read_csvy(system.file("examples", "example1.csvy", package = "csvy"))
Read csvy metadata from an external .yml/.yaml
or .json
file
read_metadata(file)
read_metadata(file)
file |
full path of file from which to read the metadata. |
the metadata as a list
Export data.frame to CSVY
write_csvy(x, file, metadata = NULL, sep = ",", dec = ".", comment_header = if (is.null(metadata)) TRUE else FALSE, name = deparse(substitute(x)), metadata_only = FALSE, ...)
write_csvy(x, file, metadata = NULL, sep = ",", dec = ".", comment_header = if (is.null(metadata)) TRUE else FALSE, name = deparse(substitute(x)), metadata_only = FALSE, ...)
x |
A data.frame. |
file |
A character string or R connection specifying a file. |
metadata |
Optionally, a character string specifying a YAML (“.yaml”) or JSON (“.json”) file to write the metadata (in lieu of including it in the header of the file). |
sep |
A character string specifying a between-field separator. Passed to |
dec |
A character string specifying a within-field separator. Passed to |
comment_header |
A logical indicating whether to comment the lines containing the YAML front matter. Default is |
name |
A character string specifying a name for the dataset. |
metadata_only |
A logical indicating whether only the metadata should be produced (no CSV component). |
... |
Additional arguments passed to |
library("datasets") write_csvy(head(iris)) # write yaml w/o comment charaters write_csvy(head(iris), comment_header = FALSE)
library("datasets") write_csvy(head(iris)) # write yaml w/o comment charaters write_csvy(head(iris), comment_header = FALSE)
Write csvy metadata to an external .yml/.yaml
or .json
file
write_metadata(metadata_list = NULL, file = NULL)
write_metadata(metadata_list = NULL, file = NULL)
metadata_list |
metadata to be stored. Must be valid as per
|
file |
full path of file in which to save the metadata. |
NULL
(invisibly)