Title: | Frictionless Standards |
---|---|
Description: | A "tabular-data-resource" (<https://specs.frictionlessdata.io/tabular-data-resource/>) is a simple format to describe a singular tabular data resource such as a CSV file. It includes support both for metadata such as author and title and a schema to describe the data, for example the types of the fields/columns in the data. Create a tabular-data-resource by providing a data.frame and specifying metadata. Write and read tabular-data-resources to and from disk. |
Authors: | Cole Brokamp [aut, cre, cph] , Tomasz Kalinowski [ctb] |
Maintainer: | Cole Brokamp <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.5.2.9000 |
Built: | 2024-11-07 19:16:15 UTC |
Source: | https://github.com/cole-brokamp/fr |
fr_tdr
object into a data frameEquivalent to as.data.frame()
; directly using tibble::as_tibble()
also works because its input is first coerced with as.data.frame()
as_data_frame(x, ...)
as_data_frame(x, ...)
x |
a |
... |
ignored |
a data frame
as_fr_tdr(mtcars, name = "mtcars") |> as_data_frame()
as_fr_tdr(mtcars, name = "mtcars") |> as_data_frame()
character
, factor
, numeric
, logical
, and Date
vectors into fr_field
objectsThe supported classes of R
objects are converted to the corresponding frictionless type
:
R class |
fr type |
character() |
string |
factor() |
string (with enum(constraints = levels(x)) ) |
numeric() , integer() |
number |
logical() |
boolean |
Date |
date |
as_fr_field(x, ...)
as_fr_field(x, ...)
x |
a character, factor, numeric, integer, logical, or Date vector |
... |
< |
a fr_field object
as_fr_field(1:10, "example_integer") # -> frictionless number as_fr_field((1:10) * 0.1, "example_double") # -> frictionless number as_fr_field(letters, "example_character") # -> frictionless string as_fr_field(factor(letters), "example_factor") # -> frictionless string with enum constraints as_fr_field(c(TRUE, FALSE, TRUE), "example_logical") # -> frictionless boolean as_fr_field(as.Date(c("2023-04-23", "2004-12-31")), "example_date") # -> frictionless date
as_fr_field(1:10, "example_integer") # -> frictionless number as_fr_field((1:10) * 0.1, "example_double") # -> frictionless number as_fr_field(letters, "example_character") # -> frictionless string as_fr_field(factor(letters), "example_factor") # -> frictionless string with enum constraints as_fr_field(c(TRUE, FALSE, TRUE), "example_logical") # -> frictionless boolean as_fr_field(as.Date(c("2023-04-23", "2004-12-31")), "example_date") # -> frictionless date
fr_tdr
objectCoerce a data frame into a fr_tdr
object
as_fr_tdr(x, ...)
as_fr_tdr(x, ...)
x |
a data.frame |
... |
< |
Use the .template
argument to provide a template fr_tdr
object from which
table-specific (i.e. "name", "version", "title", "homepage", "description")
and field-specific metadata will be copied; note that all metadata provided
in ...
will be ignored if this argument is provided
a fr_tdr object
as_fr_tdr(mtcars, name = "mtcars") S7::prop(as_fr_tdr(mtcars, name = "mtcars"), "schema")
as_fr_tdr(mtcars, name = "mtcars") S7::prop(as_fr_tdr(mtcars, name = "mtcars"), "schema")
fr_tdr
object into a listequivalent to as.list()
as_list(x, ...)
as_list(x, ...)
x |
a |
... |
ignored |
a list representing the frictionless metadata descriptor
as_fr_tdr(mtcars, name = "mtcars") |> as_list()
as_fr_tdr(mtcars, name = "mtcars") |> as_list()
Some basic dplyr functions are re-implemented here for for fr_tdr
objects.
The input is converted with as.data.frame()
before being
passed to the dplyr function. The resulting tibble object is converted back
into a fr_tdr
object, matching table- and field-specific metadata where
possible by using as_fr_tdr()
and specifying the .template
argument.
dplyr | fr |
mutate() |
fr_mutate() |
rename() |
fr_rename() |
select() |
fr_select() |
filter() |
fr_filter() |
summarise() |
fr_summarise() |
arrange() |
fr_arrange() |
fr_mutate(x, ...) fr_rename(x, ...) fr_select(x, ...) fr_filter(x, ...) fr_summarize(x, ...) fr_arrange(x, ...)
fr_mutate(x, ...) fr_rename(x, ...) fr_select(x, ...) fr_filter(x, ...) fr_summarize(x, ...) fr_arrange(x, ...)
x |
a |
... |
passed to the underlying dplyr function |
a fr_tdr
object
read_fr_tdr(fs::path_package("fr", "hamilton_poverty_2020")) |> fr_mutate(next_year = year + 1) |> fr_rename(new_year = next_year) |> fr_select(-new_year) |> fr_filter(fraction_poverty > 0.1) |> fr_summarize(median_poverty_fraction = median(fraction_poverty)) |> fr_arrange(median_poverty_fraction)
read_fr_tdr(fs::path_package("fr", "hamilton_poverty_2020")) |> fr_mutate(next_year = year + 1) |> fr_rename(new_year = next_year) |> fr_select(-new_year) |> fr_filter(fraction_poverty > 0.1) |> fr_summarize(median_poverty_fraction = median(fraction_poverty)) |> fr_arrange(median_poverty_fraction)
fr_field
objectTest if an object is a fr_field
object
is_fr_field(x)
is_fr_field(x)
x |
an object to test |
TRUE
if object is a fr_field object, FALSE
otherwise
is_fr_field(letters) is_fr_field(as_fr_field(letters, "letters"))
is_fr_field(letters) is_fr_field(as_fr_field(letters, "letters"))
read a tabular-data-resource into R
read_fr_tdr(file)
read_fr_tdr(file)
file |
Either a path to a file, a connection, or literal data (either a
single string or a raw vector). Files ending in Literal data is most useful for examples and tests. To be recognised as
literal data, wrap the input with |
A file path (or url) representing a folder
that contains a "tabular-data-resource.yaml" can
be used in file
.
a fr_tdr object
read_fr_tdr(fs::path_package("fr", "hamilton_poverty_2020"))
read_fr_tdr(fs::path_package("fr", "hamilton_poverty_2020"))
add or update field-specific metadata in a fr_tdr object
update_field(x, field, ...)
update_field(x, field, ...)
x |
a |
field |
character name of field in x to update |
... |
table schema field descriptors (e.g., |
an fr_tdr
object containing the updated field
my_mtcars <- mtcars |> as_fr_tdr(name = "mtcars") |> update_field("mpg", title = "Miles Per Gallon") S7::prop(my_mtcars, "schema")
my_mtcars <- mtcars |> as_fr_tdr(name = "mtcars") |> update_field("mpg", title = "Miles Per Gallon") S7::prop(my_mtcars, "schema")
The name
property of the fr_tdr
object is used to write a frictionless tabular-data-resource to disk. For example, if name = "my_data"
, then a folder named my_data
would be created with (1) my_data.csv
and (2) tabular-data-resource.yaml
.
write_fr_tdr(x, dir)
write_fr_tdr(x, dir)
x |
a fr_tdr object to write to disk |
dir |
path to directory where tabular-data-resource folder will be created |
x (invisibly)