knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
metar
combines several utilities, including
many packages of the core tidyverse,
the sticky package for persistent attributes,
and the data.table package for fast I/O,
to provide a comprehensive interface for working with data and its metadata.
Currently the package provides utilities for:
data.frame
s with a dplyr
-like syntaxThis package is not on CRAN (yet!), but is easy enough to install from GitHub:
# install.packages("devtools") devtools::install_github("ashiklom/metar")
Add metadata directly to objects:
library(metar) obj <- rnorm(100) metadata(obj) <- list(title = "Normal draws", description = "Some random normal draws") metadata(obj) metadata(obj, "title", "description")
You can also use the pipe-friendly add_metadata
:
obj2 <- runif(100) %>% add_metadata(title = "Normal draws") %>% # Metadata can be added or overwritten add_metadata(author = "RNG", title = "Just kidding, they're uniform draws") %>% # Metadata can be any R object add_metadata(range = list(min = 0, max = 1)) metadata(obj2) metadata(obj2, "range")
Add metadata to each column of the iris
dataset:
library(dplyr) iris_md <- iris %>% as_tibble() %>% add_column_metadata( # Metadata for individual columns Sepal.Length = list(title = "Sepal length", unit = "cm"), Sepal.Width = list(title = "Sepal width", unit = "in"), # .root sets metadata for the data frame as a whole .root = list(description = "The famous Iris dataset", author = "Not Alexey Shiklomanov") )
Metadata are stored as attributes
.
attr(iris_md, "description") attr(iris_md, "author") attr(iris_md$Sepal.Length, "title") attributes(iris_md$Sepal.Width)
These attributes are made persistent through many operations by the sticky
package.
metadata(iris_md) metadata(iris_md[1, ], "description") metadata(iris_md$Sepal.Length[1:5], "title")
The CSVY format is just a CSV file with a YAML header.
system.file("examples/example1.csvy", package = "metar") %>% readLines() %>% writeLines()
If read in with the read_csvy
function, all metadata will be automatically applied:
dat <- read_csvy(system.file("examples/example1.csvy", package = "metar")) dat metadata(dat) lapply(dat, metadata)
Note that because var2
was specified as an integer
class, it was also read in as an integer
rather than a numeric
.
class(dat$var2)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.