Title: | Read and Write CSV on the Web (CSVW) Tables and Metadata |
---|---|
Description: | Provide functions for reading and writing CSVW - i.e. CSV tables and JSON metadata. The metadata helps interpret CSV by setting the types and variable names. |
Authors: | Robin Gower |
Maintainer: | Robin Gower <[email protected]> |
License: | GPL-3 |
Version: | 0.1.7.9000 |
Built: | 2025-02-14 05:27:21 UTC |
Source: | https://github.com/robsteranium/csvwr |
Retrieve the base URI from configuration
base_uri()
base_uri()
returns the value of csvwr_base_uri
option, defaulting to example.net
## Not run: base_uri() # returns default options(csvwr_base_uri="http://www.w3.org/2013/csvw/tests/") base_uri() ## End(Not run)
## Not run: base_uri() # returns default options(csvwr_base_uri="http://www.w3.org/2013/csvw/tests/") base_uri() ## End(Not run)
Determine whether the input is true, with missing values being interpreted as false.
coalesce_truth(x)
coalesce_truth(x)
x |
logical, |
FALSE
if x is anything but TRUE
The table annotations should each be a list with keys for url
and tableSchema
.
You can use derive_table_schema
to derive a schema from a data frame.
create_metadata(tables)
create_metadata(tables)
tables |
a list of |
a list describing a tabular metadata annotation
d <- data.frame(foo="bar") table <- list(url="filename.csv", tableSchema=derive_table_schema(d)) create_metadata(tables=list(table))
d <- data.frame(foo="bar") table <- list(url="filename.csv", tableSchema=derive_table_schema(d)) create_metadata(tables=list(table))
Convert a csvw metadata to a list (csv2json)
csvw_to_list(csvw)
csvw_to_list(csvw)
csvw |
a csvw metadata list |
a list following the csv2json translation rules
## Not run: csvw_to_list(read_csvw("example.csv")) ## End(Not run)
## Not run: csvw_to_list(read_csvw("example.csv")) ## End(Not run)
The csvwr package includes some example csvw files in it's inst/extdata
directory.
You can use this function to find them.
csvwr_example(path = NULL)
csvwr_example(path = NULL)
path |
The filename. If |
Inspired by readr::readr_example()
either a file path or a vector of filenames
csvwr_example() csvwr_example("computer-scientists.csv")
csvwr_example() csvwr_example("computer-scientists.csv")
Translate csvw datatypes to R types. This implementation currently targets readr::cols column specifications.
datatype_to_type(datatypes)
datatype_to_type(datatypes)
datatypes |
a list of csvw datatypes |
a readr::cols
specification - a list of collectors
## Not run: cspec <- datatype_to_type(list("double", list(base="date", format="yyyy-MM-dd"))) readr::read_csv(readr::readr_example("challenge.csv"), col_types=cspec) ## End(Not run)
## Not run: cspec <- datatype_to_type(list("double", list(base="date", format="yyyy-MM-dd"))) readr::read_csv(readr::readr_example("challenge.csv"), col_types=cspec) ## End(Not run)
The CSVW Default Dialect specification described in CSV Dialect Description Format.
default_dialect
default_dialect
An object of class list
of length 13.
a list specifying a default csv dialect
If neither the table nor the group have a tableSchema
annotation,
then this default schema will used.
default_schema(filename, dialect = default_dialect)
default_schema(filename, dialect = default_dialect)
filename |
a csv file |
dialect |
specification of the csv's dialect (default: |
a table schema
Derive csvw metadata from a csv file
derive_metadata(filename)
derive_metadata(filename)
filename |
a csv file |
a list of csvw metadata
derive_metadata(csvwr_example("computer-scientists.csv"))
derive_metadata(csvwr_example("computer-scientists.csv"))
Derive csvw table schema from a data frame
derive_table_schema(d)
derive_table_schema(d)
d |
a data frame |
a list describing a csvw:tableSchema
derive_table_schema(data.frame(a=1,b=2))
derive_table_schema(data.frame(a=1,b=2))
Extract a referenced table from CSVW metadata
extract_table(csvw, reference)
extract_table(csvw, reference)
csvw |
the metadata |
reference |
a foreign key reference expressed as a list containing either a reference attribute or a schemaReference attribute |
a csvw table
The spec defines a normalisation process.
normalise_metadata(metadata, location)
normalise_metadata(metadata, location)
metadata |
a csvw metadata list |
location |
the location of the metadata |
metadata with normalised properties
If the argument to filename
is a json metadata document, this will be used to find csv files for
each table using the value of csvw:url
.
read_csvw(filename, metadata = NULL)
read_csvw(filename, metadata = NULL)
filename |
a path for a csv table or a json metadata document |
metadata |
optional user metadata |
If the argument to filename
is a csv file, and no metadata
is provided, an attempt is made to
derive metadata.
If the argument to filename
is a csv file, and the metadata
is provided, then the given csv will
override the value of csvw:url
.
The csvw metadata is returned as a list. In each table in the table group, an element named
dataframe
is added which provides the contents of the csv table parsed into a data frame using
the table schema.
csvw metadata list, with a dataframe
property added to each table
## Not run: read_csvw("metadata.json") read_csvw("table.csv", "metadata.json") ## End(Not run)
## Not run: read_csvw("metadata.json") read_csvw("table.csv", "metadata.json") ## End(Not run)
Wrapper around read_csvw
convenient when you're only interested in the data and there's only one table
read_csvw_dataframe(filename, metadata = NULL)
read_csvw_dataframe(filename, metadata = NULL)
filename |
a path for a csv table or a json metadata document |
metadata |
optional user metadata |
A data frame parsed using the table schema
Reads in a json document as a list, transforming columns specifications into a dataframe.
read_metadata(filename)
read_metadata(filename)
filename |
a path for a json metadata document |
csvw metadata list
Interpolate variable bindings into a URI template.
render_uri_templates(templates, bindings = NULL, ...)
render_uri_templates(templates, bindings = NULL, ...)
templates |
a character vector with URI templates |
bindings |
a list of variable bindings to be interpolated into templates |
... |
further bindings specified as named function arguments |
This doesn't yet implement the whole of RFC 6570, just enough to make the tests pass
You can bind variables by passing a list to the explicit bindings
argument,
or variadically with ...
by naming arguments according to the variable name you wish to bind.
a character vector with the expanded URI
render_uri_templates("{+url}/resource?query=value", list(url="http://example.net")) render_uri_templates("{+url}", url="http://example.net")
render_uri_templates("{+url}/resource?query=value", list(url="http://example.net")) render_uri_templates("{+url}", url="http://example.net")
As per the csvw specification for date and time formats
we accept format strings using the date field symbols defined in unicode TR35.
These are converted to POSIX 1003.1 date format strings for use in
base::strptime()
or readr::parse_date()
/readr::parse_datetime()
.
transform_datetime_format(format_string)
transform_datetime_format(format_string)
format_string |
a UAX35 date format string |
a POSIX date format string
## Not run: fmt <- transform_datetime_format("dd.MM.yyyy") strptime("01.01.2001", format=fmt) ## End(Not run)
## Not run: fmt <- transform_datetime_format("dd.MM.yyyy") strptime("01.01.2001", format=fmt) ## End(Not run)
Translate R types to csvw datatypes.
Acts as an inverse of datatype_to_type
but doesn't provide a 1:1 correspondence.
type_to_datatype(types)
type_to_datatype(types)
types |
a list of R types |
a list of csvw datatypes
Follows the csvw table validation procedure.
validate_csvw(csvw)
validate_csvw(csvw)
csvw |
a csvw metadata specification (a list) |
a validation report (list)
Fails if foreign keys aren't found in the referenced tables
validate_referential_integrity(csvw)
validate_referential_integrity(csvw)
csvw |
the metadata annotation |
a list specifying any foreign key violations