Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/leeper/csvy

Import and Export CSV Data With a YAML Metadata Header
https://github.com/leeper/csvy

cran csv csvy data r yaml

Last synced: 3 months ago
JSON representation

Import and Export CSV Data With a YAML Metadata Header

Awesome Lists containing this project

README

        

# Import and Export CSV Data With a YAML Metadata Header

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](http://csvy.org/) is simple: place a YAML header on top of a regular CSV. The yaml header is formatted according to the [Table Schema](https://frictionlessdata.io/specs/table-schema/) of a [Tabular Data Package](https://frictionlessdata.io/specs/tabular-data-package/).

A CSVY file looks like this:

```
#---
#profile: tabular-data-resource
#name: my-dataset
#path: https://raw.githubusercontent.com/csvy/csvy.github.io/master/examples/example.csvy
#title: Example file of csvy
#description: Show a csvy sample file.
#format: csvy
#mediatype: text/vnd.yaml
#encoding: utf-8
#schema:
# fields:
# - name: var1
# type: string
# - name: var2
# type: integer
# - name: var3
# type: number
#dialect:
# csvddfVersion: 1.0
# delimiter: ","
# doubleQuote: false
# lineTerminator: "\r\n"
# quoteChar: "\""
# skipInitialSpace: true
# header: true
#sources:
#- title: The csvy specifications
# path: http://csvy.org/
# email: ''
#licenses:
#- name: CC-BY-4.0
# title: Creative Commons Attribution 4.0
# path: https://creativecommons.org/licenses/by/4.0/
#---
var1,var2,var3
A,1,2.0
B,3,4.3
```

Which we can read into R like this:

```{r}
library("csvy")
str(read_csvy(system.file("examples", "example1.csvy", package = "csvy")))
```

Optional comment characters on the YAML lines make the data readable with any standard CSV parser while retaining the ability to import and export variable- and file-level metadata. The CSVY specification does not use these, but the csvy package for R does so that you (and other users) can continue to rely on `utils::read.csv()` or `readr::read_csv()` as usual. The `import()` function in [rio](https://cran.r-project.org/package=rio) supports CSVY natively.

### Export

To create a CSVY file from R, just do:

```{r}
library("csvy")
library("datasets")
write_csvy(iris, "iris.csvy")
```

It is also possible to export the metadata to separate YAML or JSON file (and then also possible to import from those separate files) by specifying the `metadata` field in `write_csvy()` and `read_csvy()`.

### Import

To read a CSVY into R, just do:

```{r}
d1 <- read_csvy("iris.csvy")
str(d1)
```

or use any other appropriate data import function to ignore the YAML metadata:

```{r}
d2 <- utils::read.table("iris.csvy", sep = ",", header = TRUE)
str(d2)
```

```{r, echo=FALSE}
unlink("iris.csvy")
```

## Package Installation

The package is available on [CRAN](https://cran.r-project.org/package=csvy) and can be installed directly in R using:

```R
install.packages("csvy")
```

The latest development version on GitHub can be installed using **devtools**:

```R
if(!require("remotes")){
install.packages("remotes")
}
remotes::install_github("leeper/csvy")
```

[![CRAN Version](http://www.r-pkg.org/badges/version/csvy)](https://cran.r-project.org/package=csvy)
![Downloads](http://cranlogs.r-pkg.org/badges/csvy)
[![Travis-CI Build Status](https://travis-ci.org/leeper/csvy.png?branch=master)](https://travis-ci.org/leeper/csvy)
[![Appveyor Build status](https://ci.appveyor.com/api/projects/status/sgttgdfcql63578u?svg=true)](https://ci.appveyor.com/project/leeper/csvy)
[![codecov.io](http://codecov.io/github/leeper/csvy/coverage.svg?branch=master)](http://codecov.io/github/leeper/csvy?branch=master)