Title: | Versatile Curation of Table Metadata |
---|---|
Description: | A YAML-based mechanism for working with table metadata. Supports compact syntax for creating, modifying, viewing, exporting, importing, displaying, and plotting metadata coded as column attributes. The 'yamlet' dialect is valid 'YAML' with defaults and conventions chosen to improve readability. See ?yamlet, ?decorate, ?modify, ?io_csv, and ?ggplot.decorated. |
Authors: | Tim Bergsma [aut, cre] |
Maintainer: | Tim Bergsma <[email protected]> |
License: | GPL-3 |
Version: | 1.2.0 |
Built: | 2024-11-26 11:45:42 UTC |
Source: | CRAN |
Coerces units to dvec.
## S3 method for class 'units' as_dvec(x, ...)
## S3 method for class 'units' as_dvec(x, ...)
x |
units |
... |
passed arguments |
library(magrittr) library(dplyr) a <- data.frame(id = 1:4, wt = c(70, 80, 70, 80), sex = c(0,1,0,1)) a %<>% decorate('wt: [ body weight, kg ]') a %<>% decorate('sex: [ sex, [ female: 0, male: 1]]') a %<>% decorate('id: identifier') a %<>% resolve a %<>% mutate(wt = as_units(wt)) a %<>% mutate(wt = as_dvec(wt)) str(a$wt)
library(magrittr) library(dplyr) a <- data.frame(id = 1:4, wt = c(70, 80, 70, 80), sex = c(0,1,0,1)) a %<>% decorate('wt: [ body weight, kg ]') a %<>% decorate('sex: [ sex, [ female: 0, male: 1]]') a %<>% decorate('id: identifier') a %<>% resolve a %<>% mutate(wt = as_units(wt)) a %<>% mutate(wt = as_dvec(wt)) str(a$wt)
Coerces dvec to units. If x has a units attribute, it is used to create class 'units'. It is an error if x has no units attribute.
## S3 method for class 'dvec' as_units(x, ..., preserve = getOption("yamlet_as_units_preserve", "label"))
## S3 method for class 'dvec' as_units(x, ..., preserve = getOption("yamlet_as_units_preserve", "label"))
x |
dvec |
... |
ignored |
preserve |
attributes to preserve; just label by default (class and units are handled implicitly) |
library(magrittr) a <- data.frame(id = 1:4, wt = c(70, 80, 70, 80), sex = c(0,1,0,1)) a %<>% decorate('wt: [ body weight, kg ]') a %<>% decorate('sex: [ sex, [ female: 0, male: 1]]') a %<>% decorate('id: identifier') a %<>% resolve a$wt %>% as_units
library(magrittr) a <- data.frame(id = 1:4, wt = c(70, 80, 70, 80), sex = c(0,1,0,1)) a %<>% decorate('wt: [ body weight, kg ]') a %<>% decorate('sex: [ sex, [ female: 0, male: 1]]') a %<>% decorate('id: identifier') a %<>% resolve a$wt %>% as_units
Coerces classified to integer.
Result is like as.integer(as.numeric(x)) + offset
but has a guide attribute: a list of integers
whose names are the original levels of x.
If you need a simple integer, consider coercing first to numeric.
## S3 method for class 'classified' as.integer( x, offset = 0L, ..., persistence = getOption("yamlet_persistence", TRUE) )
## S3 method for class 'classified' as.integer( x, offset = 0L, ..., persistence = getOption("yamlet_persistence", TRUE) )
x |
classified, see |
offset |
an integer value to add to intermediate result |
... |
passed to |
persistence |
whether to return 'dvec' (is.integer(): TRUE) or just integer. |
integer (possibly of class dvec)
Other classified:
[.classified()
,
[<-.classified()
,
[[.classified()
,
[[<-.classified()
,
c.classified()
,
classified()
,
classified.classified()
,
classified.data.frame()
,
classified.default()
,
classified.dvec()
,
classified.factor()
,
desolve.classified()
,
unclassified()
,
unclassified.classified()
,
unclassified.data.frame()
library(magrittr) # create factor with codelist attribute classified(c('knife','fork','spoon')) # give back a simple numeric classified(c('knife','fork','spoon')) %>% as.numeric # intentionally preserve levels as 'guide' attribute classified(c('knife','fork','spoon')) %>% as.integer # implement offset classified(c('knife','fork','spoon')) %>% as.integer(-1) # globally defeat the 'persistence' paradigm options(yamlet_persistence = FALSE) c('knife','fork','spoon') %>% classified %>% as.integer %>% class # integer # remove option to restore default persistence paradigm options(yamlet_persistence = NULL) c('knife','fork','spoon') %>% classified %>% as.integer %>% class # dvec # locally defeat persistence paradigm c('knife','fork','spoon') %>% classified %>% as.integer(persistence = FALSE) %>% class # integer
library(magrittr) # create factor with codelist attribute classified(c('knife','fork','spoon')) # give back a simple numeric classified(c('knife','fork','spoon')) %>% as.numeric # intentionally preserve levels as 'guide' attribute classified(c('knife','fork','spoon')) %>% as.integer # implement offset classified(c('knife','fork','spoon')) %>% as.integer(-1) # globally defeat the 'persistence' paradigm options(yamlet_persistence = FALSE) c('knife','fork','spoon') %>% classified %>% as.integer %>% class # integer # remove option to restore default persistence paradigm options(yamlet_persistence = NULL) c('knife','fork','spoon') %>% classified %>% as.integer %>% class # dvec # locally defeat persistence paradigm c('knife','fork','spoon') %>% classified %>% as.integer(persistence = FALSE) %>% class # integer
Enforces canonical attribute order for class 'decorated'. Set of default_keys will be augmented with all observed attribute names and will be expanded or reduced as necessary for each data item.
## S3 method for class 'decorated' canonical( x, default_keys = getOption("yamlet_default_keys", list("label", "guide")), ... )
## S3 method for class 'decorated' canonical( x, default_keys = getOption("yamlet_default_keys", list("label", "guide")), ... )
x |
decorated |
default_keys |
attribute names in preferred order |
... |
ignored |
decorated
Other canonical:
canonical()
,
canonical.yamlet()
Other interface:
classified.data.frame()
,
decorate.character()
,
decorate.data.frame()
,
desolve.decorated()
,
enscript.default()
,
ggplot.decorated()
,
io_csv.character()
,
io_csv.data.frame()
,
io_res.character()
,
io_res.decorated()
,
io_table.character()
,
io_table.data.frame()
,
io_yamlet.character()
,
io_yamlet.data.frame()
,
is_parseable.default()
,
mimic.default()
,
modify.default()
,
promote.list()
,
read_yamlet()
,
resolve.decorated()
,
selected.default()
,
write_yamlet()
# make some decorated data library(magrittr) x <- data.frame(x = 1, y = 1, z = factor('a')) x %<>% decorate(' x: [ guide: mm, desc: this, label: foo ] "y": [ guide: bar, desc: other ] ') # retrieve decorations: label not first! decorations(x) # sort label first by default decorations(canonical(x)) # equivalent invocation canonical(decorations(x))
# make some decorated data library(magrittr) x <- data.frame(x = 1, y = 1, z = factor('a')) x %<>% decorate(' x: [ guide: mm, desc: this, label: foo ] "y": [ guide: bar, desc: other ] ') # retrieve decorations: label not first! decorations(x) # sort label first by default decorations(canonical(x)) # equivalent invocation canonical(decorations(x))
Creates a factor of subclass 'classified',
for which there are attribute-preserving methods.
In particular, classified has a codelist attribute
indicating the origin of its levels: it is
constructed from the codelist attribute of x
if available, or from 'levels' and 'labels'
by default. Unlike the case for factor
,
length of labels cannot be one (i.e., different from
length of levels).
## Default S3 method: classified( x = character(), levels, labels, exclude = NA, ordered = is.ordered(x), nmax = NA, token = character(0), ... )
## Default S3 method: classified( x = character(), levels, labels, exclude = NA, ordered = is.ordered(x), nmax = NA, token = character(0), ... )
x |
see |
levels |
see |
labels |
see |
exclude |
see |
ordered |
see |
nmax |
see |
token |
informative label for messages |
... |
ignored |
'classified' 'factor'
Other classified:
[.classified()
,
[<-.classified()
,
[[.classified()
,
[[<-.classified()
,
as.integer.classified()
,
c.classified()
,
classified()
,
classified.classified()
,
classified.data.frame()
,
classified.dvec()
,
classified.factor()
,
desolve.classified()
,
unclassified()
,
unclassified.classified()
,
unclassified.data.frame()
# classified creates a factor with a corresponding codelist attribute classified(c('a','b','c')) # codelist 'remembers' the origins of levels classified(c('a','b','c'), labels = c('A','B','C')) # classified is 'reversible' library(magrittr) c('a','b','c') %>% classified(labels = c('A','B','C')) %>% unclassified
# classified creates a factor with a corresponding codelist attribute classified(c('a','b','c')) # codelist 'remembers' the origins of levels classified(c('a','b','c'), labels = c('A','B','C')) # classified is 'reversible' library(magrittr) c('a','b','c') %>% classified(labels = c('A','B','C')) %>% unclassified
Creates classified from factor. Uses classified.default
,
but supplies existing levels by default.
## S3 method for class 'factor' classified( x = character(), levels, labels, exclude = NA, ordered = is.ordered(x), nmax = NA, token = character(0), ... )
## S3 method for class 'factor' classified( x = character(), levels, labels, exclude = NA, ordered = is.ordered(x), nmax = NA, token = character(0), ... )
x |
see |
levels |
passed to |
labels |
passed to |
exclude |
see |
ordered |
see |
nmax |
see |
token |
informative label for messages |
... |
ignored |
'classified' 'factor'
Other classified:
[.classified()
,
[<-.classified()
,
[[.classified()
,
[[<-.classified()
,
as.integer.classified()
,
c.classified()
,
classified()
,
classified.classified()
,
classified.data.frame()
,
classified.default()
,
classified.dvec()
,
desolve.classified()
,
unclassified()
,
unclassified.classified()
,
unclassified.data.frame()
a <- factor(c('c','b','a')) levels(classified(a)) attr(classified(a), 'codelist')
a <- factor(c('c','b','a')) levels(classified(a)) attr(classified(a), 'codelist')
Captures groups as decorations for class 'data.frame'.
Creates a sequentially-valued integer attribute
with name 'groups' for each corresponding column
(after clearing all such existing designations).
It is an error if not all such columns are present.
Defaults to groups(x)
. If no columns are
specified and x has no groups, x is returned
with any existing column-level 'groups' attributes
removed.
## S3 method for class 'data.frame' decorate_groups(x, ...)
## S3 method for class 'data.frame' decorate_groups(x, ...)
x |
data.frame |
... |
unquoted names of columns to assign as groups; defaults to |
same class as x
Other decorate:
as_decorated()
,
as_decorated.default()
,
decorate()
,
decorate.character()
,
decorate.data.frame()
,
decorate.list()
,
decorate_groups()
,
decorations()
,
decorations.data.frame()
,
decorations_groups()
,
decorations_groups.data.frame()
,
group_by_decorations()
,
group_by_decorations.data.frame()
,
redecorate()
library(magrittr) library(dplyr) Theoph %>% decorate_groups(Subject, Time) %>% groups # nothing! Theoph %>% decorate_groups(Subject, Time) %>% decorations # note well Theoph %>% group_by(Subject, Time) %>% decorate_groups %>% decorations # same
library(magrittr) library(dplyr) Theoph %>% decorate_groups(Subject, Time) %>% groups # nothing! Theoph %>% decorate_groups(Subject, Time) %>% decorations # note well Theoph %>% group_by(Subject, Time) %>% decorate_groups %>% decorations # same
Treats x
as a file path. By default,
metadata is sought from a file with the same
base but the 'yaml' extension.
## S3 method for class 'character' decorate( x, meta = NULL, ..., read = getOption("yamlet_import", as.csv), ext = getOption("yamlet_extension", ".yaml") )
## S3 method for class 'character' decorate( x, meta = NULL, ..., read = getOption("yamlet_import", as.csv), ext = getOption("yamlet_extension", ".yaml") )
x |
file path for table data |
meta |
file path for corresponding yamlet metadata, or a yamlet object |
... |
passed to |
read |
function or function name for reading x |
ext |
file extension for metadata file, if relevant |
class 'decorated' 'data.frame'
Other decorate:
as_decorated()
,
as_decorated.default()
,
decorate()
,
decorate.data.frame()
,
decorate.list()
,
decorate_groups()
,
decorate_groups.data.frame()
,
decorations()
,
decorations.data.frame()
,
decorations_groups()
,
decorations_groups.data.frame()
,
group_by_decorations()
,
group_by_decorations.data.frame()
,
redecorate()
Other interface:
canonical.decorated()
,
classified.data.frame()
,
decorate.data.frame()
,
desolve.decorated()
,
enscript.default()
,
ggplot.decorated()
,
io_csv.character()
,
io_csv.data.frame()
,
io_res.character()
,
io_res.decorated()
,
io_table.character()
,
io_table.data.frame()
,
io_yamlet.character()
,
io_yamlet.data.frame()
,
is_parseable.default()
,
mimic.default()
,
modify.default()
,
promote.list()
,
read_yamlet()
,
resolve.decorated()
,
selected.default()
,
write_yamlet()
# find data file file <- system.file(package = 'yamlet', 'extdata','quinidine.csv') file # find metadata file meta <- system.file(package = 'yamlet', 'extdata','quinidine.yaml') meta # decorate with explicit metadata reference a <- decorate(file, meta) # rely on default metadata path b <- decorate(file) # in this case: same stopifnot(identical(a, b))
# find data file file <- system.file(package = 'yamlet', 'extdata','quinidine.csv') file # find metadata file meta <- system.file(package = 'yamlet', 'extdata','quinidine.yaml') meta # decorate with explicit metadata reference a <- decorate(file, meta) # rely on default metadata path b <- decorate(file) # in this case: same stopifnot(identical(a, b))
Decorates a data.frame. Expects metadata in yamlet format, and loads it onto columns as attributes.
## S3 method for class 'data.frame' decorate( x, meta = NULL, ..., persistence = getOption("yamlet_persistence", TRUE) )
## S3 method for class 'data.frame' decorate( x, meta = NULL, ..., persistence = getOption("yamlet_persistence", TRUE) )
x |
data.frame |
meta |
file path for corresponding yaml metadata, or a yamlet; an attempt will be made to guess the file path if x has a 'source' attribute |
... |
passed to |
persistence |
whether to coerce decorated columns to 'dvec' where suitable method exists |
As of v0.8.8, the data.frame method for decorate()
coerces affected columns using as_dvec
if persistence
is true and a suitable method
exists. 'vctrs' methods are implemented for class
dvec
to help attributes persist during
tidyverse operations. Details are described in
c.dvec
. Disable this functionality
with options(yamlet_persistence = FALSE)
.
class 'decorated' 'data.frame'
decorate.list
Other interface:
canonical.decorated()
,
classified.data.frame()
,
decorate.character()
,
desolve.decorated()
,
enscript.default()
,
ggplot.decorated()
,
io_csv.character()
,
io_csv.data.frame()
,
io_res.character()
,
io_res.decorated()
,
io_table.character()
,
io_table.data.frame()
,
io_yamlet.character()
,
io_yamlet.data.frame()
,
is_parseable.default()
,
mimic.default()
,
modify.default()
,
promote.list()
,
read_yamlet()
,
resolve.decorated()
,
selected.default()
,
write_yamlet()
Other decorate:
as_decorated()
,
as_decorated.default()
,
decorate()
,
decorate.character()
,
decorate.list()
,
decorate_groups()
,
decorate_groups.data.frame()
,
decorations()
,
decorations.data.frame()
,
decorations_groups()
,
decorations_groups.data.frame()
,
group_by_decorations()
,
group_by_decorations.data.frame()
,
redecorate()
# find data path library(csv) file <- system.file(package = 'yamlet', 'extdata','quinidine.csv') file dat <- as.csv(file) # dat now has 'source' attribute # use source attribute to find metadata a <- decorate(as.csv(file)) # supply metadata path (or something close) explicitly b <- decorate(dat, meta = file) # these are equivalent stopifnot(identical(a, b))
# find data path library(csv) file <- system.file(package = 'yamlet', 'extdata','quinidine.csv') file dat <- as.csv(file) # dat now has 'source' attribute # use source attribute to find metadata a <- decorate(as.csv(file)) # supply metadata path (or something close) explicitly b <- decorate(dat, meta = file) # these are equivalent stopifnot(identical(a, b))
Recovers groups decorations for class 'data.frame'.
Seeks a sequentially-valued integer attribute
with name 'groups' for each column, sorts these,
and returns a character vector like group_vars(x)
.
## S3 method for class 'data.frame' decorations_groups(x, ...)
## S3 method for class 'data.frame' decorations_groups(x, ...)
x |
data.frame |
... |
ignored |
character: names of groups columns
Other decorate:
as_decorated()
,
as_decorated.default()
,
decorate()
,
decorate.character()
,
decorate.data.frame()
,
decorate.list()
,
decorate_groups()
,
decorate_groups.data.frame()
,
decorations()
,
decorations.data.frame()
,
decorations_groups()
,
group_by_decorations()
,
group_by_decorations.data.frame()
,
redecorate()
library(magrittr) library(dplyr) Theoph %<>% group_by(Subject, Time) Theoph %>% group_vars Theoph %>% decorations_groups # nothing! Theoph %<>% decorate_groups Theoph %>% decorations_groups # something! Theoph %<>% ungroup Theoph %>% group_vars # gone! Theoph %<>% group_by(across(all_of(decorations_groups(.)))) Theoph %>% group_vars # recovered! Theoph %<>% group_by_decorations Theoph %>% group_vars # same rm(Theoph)
library(magrittr) library(dplyr) Theoph %<>% group_by(Subject, Time) Theoph %>% group_vars Theoph %>% decorations_groups # nothing! Theoph %<>% decorate_groups Theoph %>% decorations_groups # something! Theoph %<>% ungroup Theoph %>% group_vars # gone! Theoph %<>% group_by(across(all_of(decorations_groups(.)))) Theoph %>% group_vars # recovered! Theoph %<>% group_by_decorations Theoph %>% group_vars # same rm(Theoph)
Retrieve the decorations of a data.frame; i.e., the metadata used to decorate it. Returns a list with same names as the data.frame. By default, 'class' and 'level' attributes are excluded from the result, as you likely don't want to manipulate these independently.
## S3 method for class 'data.frame' decorations( x, ..., exclude_attr = getOption("yamlet_exclude_attr", c("class", "levels")) )
## S3 method for class 'data.frame' decorations( x, ..., exclude_attr = getOption("yamlet_exclude_attr", c("class", "levels")) )
x |
data.frame |
... |
optional unquoted column names to limit output (passed to |
exclude_attr |
attributes to remove from the result |
named list of class 'yamlet'
Other decorate:
as_decorated()
,
as_decorated.default()
,
decorate()
,
decorate.character()
,
decorate.data.frame()
,
decorate.list()
,
decorate_groups()
,
decorate_groups.data.frame()
,
decorations()
,
decorations_groups()
,
decorations_groups.data.frame()
,
group_by_decorations()
,
group_by_decorations.data.frame()
,
redecorate()
# prepare a decorated data.frame file <- system.file(package = 'yamlet', 'extdata','quinidine.csv') x <- decorate(file) # retrieve the decorations decorations(x, Subject, time, conc)
# prepare a decorated data.frame file <- system.file(package = 'yamlet', 'extdata','quinidine.csv') x <- decorate(file) # retrieve the decorations decorations(x, Subject, time, conc)
Un-resolves explicit usage of default key 'guide' to
implicit usage for class 'classified'.
Calls
drop_title
(a non-action by default),
unclassified
,
followed by implicit_guide
.
## S3 method for class 'classified' desolve(x, ...)
## S3 method for class 'classified' desolve(x, ...)
x |
classified |
... |
passed to |
dvec
Other resolve:
desolve()
,
desolve.data.frame()
,
desolve.decorated()
,
desolve.dvec()
,
resolve()
,
resolve.classified()
,
resolve.data.frame()
,
resolve.decorated()
,
resolve.dvec()
Other classified:
[.classified()
,
[<-.classified()
,
[[.classified()
,
[[<-.classified()
,
as.integer.classified()
,
c.classified()
,
classified()
,
classified.classified()
,
classified.data.frame()
,
classified.default()
,
classified.dvec()
,
classified.factor()
,
unclassified()
,
unclassified.classified()
,
unclassified.data.frame()
library(magrittr) x <- as_dvec( 4:6, guide = list(a = 4L, b = 5L, c = 6L) ) # untouched x %>% str # resolved x %>% resolve %>% str # resolved and desolved x %>% resolve %>% desolve %>% str
library(magrittr) x <- as_dvec( 4:6, guide = list(a = 4L, b = 5L, c = 6L) ) # untouched x %>% str # resolved x %>% resolve %>% str # resolved and desolved x %>% resolve %>% desolve %>% str
Un-resolves explicit usage of default key 'guide' to
implicit usage for 'decorated' class.
Simply calls
drop_title
,
unclassified
,
and implicit_guide
.
## S3 method for class 'decorated' desolve(x, ...)
## S3 method for class 'decorated' desolve(x, ...)
x |
decorated |
... |
passed to |
decorated
Other resolve:
desolve()
,
desolve.classified()
,
desolve.data.frame()
,
desolve.dvec()
,
resolve()
,
resolve.classified()
,
resolve.data.frame()
,
resolve.decorated()
,
resolve.dvec()
Other interface:
canonical.decorated()
,
classified.data.frame()
,
decorate.character()
,
decorate.data.frame()
,
enscript.default()
,
ggplot.decorated()
,
io_csv.character()
,
io_csv.data.frame()
,
io_res.character()
,
io_res.decorated()
,
io_table.character()
,
io_table.data.frame()
,
io_yamlet.character()
,
io_yamlet.data.frame()
,
is_parseable.default()
,
mimic.default()
,
modify.default()
,
promote.list()
,
read_yamlet()
,
resolve.decorated()
,
selected.default()
,
write_yamlet()
library(magrittr) file <- system.file(package = 'yamlet', 'extdata','quinidine.csv') x <- decorate(file) # this is how Age, glyco, Race look when resolved x %>% resolve %>% decorations(Age, glyco, Race) # we can resolve two of them and then 'unresolve' all of them x %>% resolve(glyco, Race) %>% desolve %>% decorations(Age, glyco, Race)
library(magrittr) file <- system.file(package = 'yamlet', 'extdata','quinidine.csv') x <- decorate(file) # this is how Age, glyco, Race look when resolved x %>% resolve %>% decorations(Age, glyco, Race) # we can resolve two of them and then 'unresolve' all of them x %>% resolve(glyco, Race) %>% desolve %>% decorations(Age, glyco, Race)
Modifies specific attributes of each indicated element (all elements by default).
## Default S3 method: enscript( x, ..., open = getOption("yamlet_append_units_open", " ("), close = getOption("yamlet_append_units_close", ")"), format = getOption("yamlet_format", ifelse(knitr::is_latex_output(), "latex", "html")) )
## Default S3 method: enscript( x, ..., open = getOption("yamlet_append_units_open", " ("), close = getOption("yamlet_append_units_close", ")"), format = getOption("yamlet_format", ifelse(knitr::is_latex_output(), "latex", "html")) )
x |
object |
... |
indicated columns, or name-value pairs; passed to |
open |
character to precede units |
close |
character to follow units |
format |
one of 'latex' or 'html' |
The goal here is to render labels and units (where present) in a way that supports subscripts and superscripts for both plots and tables in either html or latex contexts.
The current implementation writes an 'expression' attribute
to support figure labels and a 'title' attribute to support
tables. print.decorated_ggplot
will attempt
to honor the expression attribute if it exists.
tablet.data.frame
will attempt to honor
the title attribute if it exists (see Details there).
An attempt is made to guess the output format (html or latex).
In addition to the 'title' and 'expression' attributes, enscript() writes
a 'plotmath' attribute to store plotmath versions of factor levels,
where present. By default, factor levels are converted to their
latex or html equivalents. However, print.decorated_ggplot
will use the plotmath versions of factor labels for legends and
facet labels. If a 'plotmath' attribute already exists, it is not
overwritten, preventing
the same variable from being accidentally transformed twice.
To flexibly support latex, html, and plotmath, this function
expects column labels and units to be encoded in "spork" syntax.
See as_spork
for details and examples.
Briefly, "_" precedes a subscript, "^" precedes a superscript,
and "." is used to force the termination of either a
superscript or a subscript where necessary. For best results,
units should be written using *, /, and ^; e.g. "kg*m^2/s^2"
not "kg m2 s-2" (although both are valid:
see is_parseable
). A literal backslash followed by "n"
represents a newline. Greek letters are represented by their names,
except where names are enclosed in backticks.
enscript()
always calls resolve()
for the indicated
columns, to make units present where appropriate.
'enscript', a superclass of x
Other enscript:
enscript()
Other interface:
canonical.decorated()
,
classified.data.frame()
,
decorate.character()
,
decorate.data.frame()
,
desolve.decorated()
,
ggplot.decorated()
,
io_csv.character()
,
io_csv.data.frame()
,
io_res.character()
,
io_res.decorated()
,
io_table.character()
,
io_table.data.frame()
,
io_yamlet.character()
,
io_yamlet.data.frame()
,
is_parseable.default()
,
mimic.default()
,
modify.default()
,
promote.list()
,
read_yamlet()
,
resolve.decorated()
,
selected.default()
,
write_yamlet()
library(magrittr) library(ggplot2) x <- data.frame(time = 1:10, work = (1:10)^1.5) x %<>% decorate(' time: [ Time_elapsed, h ] work: [ Work_total_observed, kg*m^2/s^2 ] ') x %>% decorations x %>% ggplot(aes(time, work)) + geom_point() x %>% enscript %>% ggplot(aes(time, work)) + geom_point() x %>% enscript(format = 'html') %$% work %>% attr('title') testthat::expect_equal(enscript(x), enscript(enscript(x)))
library(magrittr) library(ggplot2) x <- data.frame(time = 1:10, work = (1:10)^1.5) x %<>% decorate(' time: [ Time_elapsed, h ] work: [ Work_total_observed, kg*m^2/s^2 ] ') x %>% decorations x %>% ggplot(aes(time, work)) + geom_point() x %>% enscript %>% ggplot(aes(time, work)) + geom_point() x %>% enscript(format = 'html') %$% work %>% attr('title') testthat::expect_equal(enscript(x), enscript(enscript(x)))
Creates a new ggplot object for a decorated data.frame.
This is the ggplot() method for class 'decorated'.
It creates a ggplot object using the default method,
but reclassifies it as 'decorated_ggplot' so that a custom print method
is invoked; see print.decorated_ggplot
.
## S3 method for class 'decorated' ggplot(data, ...)
## S3 method for class 'decorated' ggplot(data, ...)
data |
decorated, see |
... |
passed to |
This approach is similar to but more flexible than
the method for ggready
.
For fine control, you can switch between 'data.frame'
and 'decorated' using as_decorated
(supplies null decorations) and as.data.frame
(preserves decorations).
return value like ggplot
but inheriting 'decorated_ggplot'
decorate resolve ggready
Other decorated_ggplot:
ggplot_build.decorated_ggplot()
,
print.decorated_ggplot()
Other interface:
canonical.decorated()
,
classified.data.frame()
,
decorate.character()
,
decorate.data.frame()
,
desolve.decorated()
,
enscript.default()
,
io_csv.character()
,
io_csv.data.frame()
,
io_res.character()
,
io_res.decorated()
,
io_table.character()
,
io_table.data.frame()
,
io_yamlet.character()
,
io_yamlet.data.frame()
,
is_parseable.default()
,
mimic.default()
,
modify.default()
,
promote.list()
,
read_yamlet()
,
resolve.decorated()
,
selected.default()
,
write_yamlet()
file <- system.file(package = 'yamlet', 'extdata','quinidine.csv') library(ggplot2) library(dplyr) library(magrittr) # par(ask = FALSE) x <- decorate(file) x %<>% filter(!is.na(conc)) # Manipulate class to switch among ggplot methods. class(x) class(data.frame(x)) class(as_decorated(data.frame(x))) # The bare data.frame gives boring labels and un-ordered groups. map <- aes(x = time, y = conc, color = Heart) data.frame(x) %>% ggplot(map) + geom_point() # Decorated data.frame uses supplied labels. # Notice CHF levels are still not ordered. (Moderate first.) x %>% ggplot(map) + geom_point() # If we resolve Heart, CHF levels are ordered. x %<>% resolve(Heart) x %>% ggplot(map) + geom_point() # We can map aesthetics as decorations. x %<>% decorate('Heart: [ color: [gold, purple, green]]') x %>% ggplot(map) + geom_point() # Colors are matched to particular levels. Purple drops out here: x %>% filter(Heart != 'Moderate') %>% ggplot(map) + geom_point() # We can resolve other columns for a chance to enrich the output with units. x %<>% resolve suppressWarnings( # because this complains for columns with no units x <- modify(x, title = paste0(label, '\n(', units, ')')) ) x %>% ggplot(map) + geom_point() # Or something fancier. x %<>% modify(conc, title = 'conc_serum. (mg*L^-1.)') x %>% ggplot(map) + geom_point() # The y-axis title is deliberately given in spork syntax for elegant coercion: library(spork) x %<>% modify(conc, expression = as.expression(as_plotmath(as_spork(title)))) x %>% ggplot(map) + geom_point() # Add a fancier label for Heart, and facet by a factor: x %<>% modify(Heart, expression = as.expression(as_plotmath(as_spork('CHF^\\*')))) x %>% ggplot(map) + geom_point() + facet_wrap(~Creatinine) # ggready handles the units and plotmath implicitly for a 'standard' display: x %>% ggready %>% ggplot(map) + geom_point() + facet_wrap(~Creatinine) # Notice that instead of over-writing the label # attribute, we are creating a stack of label # substitutes (title, expression) so that # label is still available as an argument # if we want to try something else. The # print method by default looks for all of these. # Precedence is expression, title, label, column name. # Precedence can be controlled using # options(decorated_ggplot_search = c(a, b, ...) ). # Here we try a dataset with conditional labels and units. file <- system.file(package = 'yamlet', 'extdata','phenobarb.csv') x <- file %>% decorate %>% resolve # Note that value has two elements for label and guide. x %>% decorations(value) # The print method defaults to the first, with warning. map <- aes(x = time, y = value, color = event) x %>% ggplot(map) + geom_point() # If we subset appropriately, the relevant value is substituted. x %>% filter(event == 'conc') %>% ggplot(map) + geom_point() x %>% filter(event == 'conc') %>% ggplot(aes(x = time, y = value, color = ApgarInd)) + geom_point() x %>% filter(event == 'dose') %>% ggplot(aes(x = time, y = value, color = Wt)) + geom_point() + scale_y_log10() + scale_color_gradientn(colours = rainbow(4)) # print.decorated_ggplot will attempt to honor coordinated aesthetics. x <- data.frame(x = c(1:6, 3:8), y = c(1:6,1:6), z = letters[c(1:6,1:6)]) x %<>% decorate('z: [color: ["red", "blue", "green", "gold", "black", "magenta"]]') x %<>% decorate('z: [fill: ["red", "blue", "green", "gold", "black", "magenta"]]') x %<>% decorate('z: [shape: [20, 21, 22, 23, 24, 25]]') x %<>% decorate('z: [linetype: [6, 5, 4, 3, 2, 1]]') x %<>% decorate('z: [alpha: [ .9, .8, .7, .6, .5, .4]]') x %<>% decorate('z: [size: [1, 1.5, 2, 2.5, 3, 3.5]]') x %>% ggplot(aes( x, y, color = z, fill = z, shape = z, linetype = z, alpha = z, size = z, )) + geom_point() + geom_line(size = 1)
file <- system.file(package = 'yamlet', 'extdata','quinidine.csv') library(ggplot2) library(dplyr) library(magrittr) # par(ask = FALSE) x <- decorate(file) x %<>% filter(!is.na(conc)) # Manipulate class to switch among ggplot methods. class(x) class(data.frame(x)) class(as_decorated(data.frame(x))) # The bare data.frame gives boring labels and un-ordered groups. map <- aes(x = time, y = conc, color = Heart) data.frame(x) %>% ggplot(map) + geom_point() # Decorated data.frame uses supplied labels. # Notice CHF levels are still not ordered. (Moderate first.) x %>% ggplot(map) + geom_point() # If we resolve Heart, CHF levels are ordered. x %<>% resolve(Heart) x %>% ggplot(map) + geom_point() # We can map aesthetics as decorations. x %<>% decorate('Heart: [ color: [gold, purple, green]]') x %>% ggplot(map) + geom_point() # Colors are matched to particular levels. Purple drops out here: x %>% filter(Heart != 'Moderate') %>% ggplot(map) + geom_point() # We can resolve other columns for a chance to enrich the output with units. x %<>% resolve suppressWarnings( # because this complains for columns with no units x <- modify(x, title = paste0(label, '\n(', units, ')')) ) x %>% ggplot(map) + geom_point() # Or something fancier. x %<>% modify(conc, title = 'conc_serum. (mg*L^-1.)') x %>% ggplot(map) + geom_point() # The y-axis title is deliberately given in spork syntax for elegant coercion: library(spork) x %<>% modify(conc, expression = as.expression(as_plotmath(as_spork(title)))) x %>% ggplot(map) + geom_point() # Add a fancier label for Heart, and facet by a factor: x %<>% modify(Heart, expression = as.expression(as_plotmath(as_spork('CHF^\\*')))) x %>% ggplot(map) + geom_point() + facet_wrap(~Creatinine) # ggready handles the units and plotmath implicitly for a 'standard' display: x %>% ggready %>% ggplot(map) + geom_point() + facet_wrap(~Creatinine) # Notice that instead of over-writing the label # attribute, we are creating a stack of label # substitutes (title, expression) so that # label is still available as an argument # if we want to try something else. The # print method by default looks for all of these. # Precedence is expression, title, label, column name. # Precedence can be controlled using # options(decorated_ggplot_search = c(a, b, ...) ). # Here we try a dataset with conditional labels and units. file <- system.file(package = 'yamlet', 'extdata','phenobarb.csv') x <- file %>% decorate %>% resolve # Note that value has two elements for label and guide. x %>% decorations(value) # The print method defaults to the first, with warning. map <- aes(x = time, y = value, color = event) x %>% ggplot(map) + geom_point() # If we subset appropriately, the relevant value is substituted. x %>% filter(event == 'conc') %>% ggplot(map) + geom_point() x %>% filter(event == 'conc') %>% ggplot(aes(x = time, y = value, color = ApgarInd)) + geom_point() x %>% filter(event == 'dose') %>% ggplot(aes(x = time, y = value, color = Wt)) + geom_point() + scale_y_log10() + scale_color_gradientn(colours = rainbow(4)) # print.decorated_ggplot will attempt to honor coordinated aesthetics. x <- data.frame(x = c(1:6, 3:8), y = c(1:6,1:6), z = letters[c(1:6,1:6)]) x %<>% decorate('z: [color: ["red", "blue", "green", "gold", "black", "magenta"]]') x %<>% decorate('z: [fill: ["red", "blue", "green", "gold", "black", "magenta"]]') x %<>% decorate('z: [shape: [20, 21, 22, 23, 24, 25]]') x %<>% decorate('z: [linetype: [6, 5, 4, 3, 2, 1]]') x %<>% decorate('z: [alpha: [ .9, .8, .7, .6, .5, .4]]') x %<>% decorate('z: [size: [1, 1.5, 2, 2.5, 3, 3.5]]') x %>% ggplot(aes( x, y, color = z, fill = z, shape = z, linetype = z, alpha = z, size = z, )) + geom_point() + geom_line(size = 1)
Invokes group_by
using whatever groups are recovered by
decorations_groups
.
## S3 method for class 'data.frame' group_by_decorations(x, ...)
## S3 method for class 'data.frame' group_by_decorations(x, ...)
x |
grouped_df |
... |
ignored |
list of symbols
Other decorate:
as_decorated()
,
as_decorated.default()
,
decorate()
,
decorate.character()
,
decorate.data.frame()
,
decorate.list()
,
decorate_groups()
,
decorate_groups.data.frame()
,
decorations()
,
decorations.data.frame()
,
decorations_groups()
,
decorations_groups.data.frame()
,
group_by_decorations()
,
redecorate()
library(magrittr) library(dplyr) Theoph %>% group_vars # nothing! Theoph %<>% decorate_groups(Subject, Time) Theoph %<>% group_by_decorations Theoph %>% group_vars # something rm(Theoph)
library(magrittr) library(dplyr) Theoph %>% group_vars # nothing! Theoph %<>% decorate_groups(Subject, Time) Theoph %<>% group_by_decorations Theoph %>% group_vars # something rm(Theoph)
Imports or exports documented tables as comma-separated variable.
Generic, with methods that extend as.csv
.
io_csv(x, ...)
io_csv(x, ...)
x |
object |
... |
passed arguments |
See methods.
Other io:
io_csv.character()
,
io_csv.data.frame()
,
io_res()
,
io_res.character()
,
io_res.decorated()
,
io_table()
,
io_table.character()
,
io_table.data.frame()
,
io_yamlet()
,
io_yamlet.character()
,
io_yamlet.data.frame()
,
io_yamlet.yamlet()
# generate some decorated data file <- system.file(package = 'yamlet', 'extdata','quinidine.csv') x <- decorate(file) # get a temporary filepath out <- file.path(tempdir(), 'out.csv') # save file using io_csv (returns filepath) foo <- io_csv(x, out) stopifnot(identical(out, foo)) # read using this filepath y <- io_csv(foo) # lossless round-trip (ignoring source attribute) attr(x, 'source') <- NULL attr(y, 'source') <- NULL stopifnot(identical(x, y))
# generate some decorated data file <- system.file(package = 'yamlet', 'extdata','quinidine.csv') x <- decorate(file) # get a temporary filepath out <- file.path(tempdir(), 'out.csv') # save file using io_csv (returns filepath) foo <- io_csv(x, out) stopifnot(identical(out, foo)) # read using this filepath y <- io_csv(foo) # lossless round-trip (ignoring source attribute) attr(x, 'source') <- NULL attr(y, 'source') <- NULL stopifnot(identical(x, y))
Imports or exports documented tables. Generic, with methods
that extend read.table
and write.table
.
io_table(x, ...)
io_table(x, ...)
x |
object |
... |
passed arguments |
See methods.
Other io:
io_csv()
,
io_csv.character()
,
io_csv.data.frame()
,
io_res()
,
io_res.character()
,
io_res.decorated()
,
io_table.character()
,
io_table.data.frame()
,
io_yamlet()
,
io_yamlet.character()
,
io_yamlet.data.frame()
,
io_yamlet.yamlet()
# generate some decorated data file <- system.file(package = 'yamlet', 'extdata','quinidine.csv') x <- decorate(file) # get a temporary filepath out <- file.path(tempdir(), 'out.tab') # save file using io_table (returns filepath) foo <- io_table(x, out) stopifnot(identical(out, foo)) # read using this filepath y <- io_table(foo, as.is = TRUE) # lossless round-trip attr(x, 'source') <- NULL rownames(x) <- NULL rownames(y) <- NULL stopifnot(identical(x, y))
# generate some decorated data file <- system.file(package = 'yamlet', 'extdata','quinidine.csv') x <- decorate(file) # get a temporary filepath out <- file.path(tempdir(), 'out.tab') # save file using io_table (returns filepath) foo <- io_table(x, out) stopifnot(identical(out, foo)) # read using this filepath y <- io_table(foo, as.is = TRUE) # lossless round-trip attr(x, 'source') <- NULL rownames(x) <- NULL rownames(y) <- NULL stopifnot(identical(x, y))
Tests whether x inherits 'dvec'.
is_dvec(x)
is_dvec(x)
x |
object |
logical
is_dvec(1L) is_dvec(as_dvec(1L))
is_dvec(1L) is_dvec(as_dvec(1L))
Tries to mimic another vector or factor. If meaningful and possible, x acquires a guide attribute with labels from corresponding values in y. Any codelist attribute is removed. No guide is created for zero-length x. If x is a factor, unused levels are removed.
## Default S3 method: mimic(x, y = x, ...)
## Default S3 method: mimic(x, y = x, ...)
x |
vector-like |
y |
vector-like, same length as x |
... |
passed to |
same class as x
Other mimic:
mimic()
,
mimic.classified()
Other interface:
canonical.decorated()
,
classified.data.frame()
,
decorate.character()
,
decorate.data.frame()
,
desolve.decorated()
,
enscript.default()
,
ggplot.decorated()
,
io_csv.character()
,
io_csv.data.frame()
,
io_res.character()
,
io_res.decorated()
,
io_table.character()
,
io_table.data.frame()
,
io_yamlet.character()
,
io_yamlet.data.frame()
,
is_parseable.default()
,
modify.default()
,
promote.list()
,
read_yamlet()
,
resolve.decorated()
,
selected.default()
,
write_yamlet()
library(magrittr) library(dplyr) let <- letters[1:5] LET <- LETTERS[1:5] int <- 0L:4L num <- as.numeric(int) fac <- factor(let) css <- classified(let) # any of these can mimic any other str(mimic(LET, let)) str(mimic(num, let)) str(mimic(let, num)) # factors get a guide and classifieds get a named codelist str(mimic(fac, int)) str(mimic(css, int)) # int can 'pick up' the factor levels as guide names str(mimic(int, css)) # if two variables mean essentially the same thing, # mimic lets you save space x <- data.frame(id = 1:2, ID = c('A','B')) x x %<>% mutate(id = mimic(id, ID)) %>% select(-ID) x # ID still available, in principle: x %>% as_decorated %>% resolve
library(magrittr) library(dplyr) let <- letters[1:5] LET <- LETTERS[1:5] int <- 0L:4L num <- as.numeric(int) fac <- factor(let) css <- classified(let) # any of these can mimic any other str(mimic(LET, let)) str(mimic(num, let)) str(mimic(let, num)) # factors get a guide and classifieds get a named codelist str(mimic(fac, int)) str(mimic(css, int)) # int can 'pick up' the factor levels as guide names str(mimic(int, css)) # if two variables mean essentially the same thing, # mimic lets you save space x <- data.frame(id = 1:2, ID = c('A','B')) x x %<>% mutate(id = mimic(id, ID)) %>% select(-ID) x # ID still available, in principle: x %>% as_decorated %>% resolve
Modifies the attributes of each indicated element (all elements by default). Tries to assign the value of an expression to the supplied label, with existing attributes and the object itself (.) available as arguments. Gives a warning if the supplied label is considered reserved. Intends to support anything with one or more non-empty names.
## Default S3 method: modify( x, ..., .reserved = getOption("yamlet_modify_reserved", c("class", "levels", "labels", "names")) )
## Default S3 method: modify( x, ..., .reserved = getOption("yamlet_modify_reserved", c("class", "levels", "labels", "names")) )
x |
object |
... |
indicated columns, or name-value pairs |
.reserved |
reserved labels that warn on assignment |
The name of the component itself is available during assignments as
attribute 'name' (any pre-existing attribute 'name' is temporarily masked).
After all assignments are complete, the value of 'name' is enforced at the object level.
Thus, modify
expressions can modify component names.
As currently implemented, the expression is evaluated by
eval_tidy
, with attributes supplied as
the data
argument. Thus, names in the expression
may be disambiguated, e.g. with .data
. See examples.
same class as x
Other modify:
modify()
,
named()
,
selected()
,
selected.default()
Other interface:
canonical.decorated()
,
classified.data.frame()
,
decorate.character()
,
decorate.data.frame()
,
desolve.decorated()
,
enscript.default()
,
ggplot.decorated()
,
io_csv.character()
,
io_csv.data.frame()
,
io_res.character()
,
io_res.decorated()
,
io_table.character()
,
io_table.data.frame()
,
io_yamlet.character()
,
io_yamlet.data.frame()
,
is_parseable.default()
,
mimic.default()
,
promote.list()
,
read_yamlet()
,
resolve.decorated()
,
selected.default()
,
write_yamlet()
library(magrittr) library(dplyr) file <- system.file(package = 'yamlet', 'extdata','quinidine.csv') x <- decorate(file) # modify selected columns x %<>% modify(title = paste(label, '(', guide, ')'), time) x %>% select(time, conc) %>% decorations # modify (almost) all columns x %<>% modify(title = paste(label, '(', guide, ')'), -Subject) x %>% select(time, conc) %>% decorations # use column itself x %<>% modify(`defined values` = sum(!is.na(.))) x %>% select(time) %>% decorations # rename column x %<>% modify(time, name = label) names(x) # warn if assignment fails ## Not run: \donttest{ x %<>% modify(title = foo, time) } ## End(Not run) # support lists list(a = 1, b = 1:10, c = letters) %>% modify(length = length(.), b:c) x %<>% select(Subject) %>% modify(label = NULL, `defined values` = NULL) # distinguish data and environment location <- 'environment' x %>% modify(where = location) %>% decorations x %>% modify(where = .env$location) %>% decorations ## Not run: \donttest{ x%>% modify(where = .data$location) %>% decorations } ## End(Not run) x %>% modify(location = 'attributes', where = location) %>% decorations x %>% modify(location = 'attributes', where = .data$location) %>% decorations
library(magrittr) library(dplyr) file <- system.file(package = 'yamlet', 'extdata','quinidine.csv') x <- decorate(file) # modify selected columns x %<>% modify(title = paste(label, '(', guide, ')'), time) x %>% select(time, conc) %>% decorations # modify (almost) all columns x %<>% modify(title = paste(label, '(', guide, ')'), -Subject) x %>% select(time, conc) %>% decorations # use column itself x %<>% modify(`defined values` = sum(!is.na(.))) x %>% select(time) %>% decorations # rename column x %<>% modify(time, name = label) names(x) # warn if assignment fails ## Not run: \donttest{ x %<>% modify(title = foo, time) } ## End(Not run) # support lists list(a = 1, b = 1:10, c = letters) %>% modify(length = length(.), b:c) x %<>% select(Subject) %>% modify(label = NULL, `defined values` = NULL) # distinguish data and environment location <- 'environment' x %>% modify(where = location) %>% decorations x %>% modify(where = .env$location) %>% decorations ## Not run: \donttest{ x%>% modify(where = .data$location) %>% decorations } ## End(Not run) x %>% modify(location = 'attributes', where = location) %>% decorations x %>% modify(location = 'attributes', where = .data$location) %>% decorations
Default labels (e.g. mappings for x
, y
, etc.)
will be used to search data
for more meaningful
labels, taking first available from attributes
with names in search
. Likewise, if mappings for
colour (color), fill, size, etc. (see defaults for discrete
)
indicate columns that have these defined as attributes,
an attempt is made to add a corresponding discrete scale if
one does not exist already. Values are recycled if necessary
and are specific by ordinal position to the corresponding
level of the corresponding variable. Levels are defined
in increasing priority by
sort(unique(x))
,
any guide attribute,
any factor levels,
any codelist attribute, or
any plotmath attribute.
## S3 method for class 'decorated_ggplot' print( x, ..., search = getOption("yamlet_decorated_ggplot_search", c("expression", "title", "label")), discrete = getOption("yamlet_decorated_ggplot_discrete", c("colour", "fill", "size", "shape", "linetype", "linewidth", "alpha")), drop = getOption("yamlet_decorated_ggplot_drop", TRUE) )
## S3 method for class 'decorated_ggplot' print( x, ..., search = getOption("yamlet_decorated_ggplot_search", c("expression", "title", "label")), discrete = getOption("yamlet_decorated_ggplot_discrete", c("colour", "fill", "size", "shape", "linetype", "linewidth", "alpha")), drop = getOption("yamlet_decorated_ggplot_drop", TRUE) )
x |
class 'decorated_ggplot' from |
... |
ignored |
search |
attribute names from which to seek label substitutes |
discrete |
discrete aesthetics to map from data decorations where available |
drop |
should unused factor levels be omitted from data-driven discrete scales? |
see print.ggplot
Other decorated_ggplot:
ggplot.decorated()
,
ggplot_build.decorated_ggplot()
example(ggplot.decorated)
example(ggplot.decorated)
Reads yamlet from file.
Similar to io_yamlet.character
but also reads text fragments.
read_yamlet( x, ..., default_keys = getOption("yamlet_default_keys", list("label", "guide")) )
read_yamlet( x, ..., default_keys = getOption("yamlet_default_keys", list("label", "guide")) )
x |
file path for yamlet, or vector of yamlet in storage syntax |
... |
passed to |
default_keys |
character: default keys for the first n anonymous members of each element |
yamlet: a named list with default keys applied
Other interface:
canonical.decorated()
,
classified.data.frame()
,
decorate.character()
,
decorate.data.frame()
,
desolve.decorated()
,
enscript.default()
,
ggplot.decorated()
,
io_csv.character()
,
io_csv.data.frame()
,
io_res.character()
,
io_res.decorated()
,
io_table.character()
,
io_table.data.frame()
,
io_yamlet.character()
,
io_yamlet.data.frame()
,
is_parseable.default()
,
mimic.default()
,
modify.default()
,
promote.list()
,
resolve.decorated()
,
selected.default()
,
write_yamlet()
library(csv) file <- system.file(package = 'yamlet', 'extdata','quinidine.csv') meta <- system.file(package = 'yamlet', 'extdata','quinidine.yaml') x <- as.csv(file) y <- read_yamlet(meta) x <- decorate(x, meta = y) stopifnot(identical(x, decorate(file)))
library(csv) file <- system.file(package = 'yamlet', 'extdata','quinidine.csv') meta <- system.file(package = 'yamlet', 'extdata','quinidine.yaml') x <- as.csv(file) y <- read_yamlet(meta) x <- decorate(x, meta = y) stopifnot(identical(x, decorate(file)))
Resolves implicit usage of default key 'guide' to
explicit usage for decorated class.
Calls
explicit_guide
,
classified
, and
make_title
.
## S3 method for class 'decorated' resolve(x, ...)
## S3 method for class 'decorated' resolve(x, ...)
x |
decorated |
... |
passed to |
decorated
Other resolve:
desolve()
,
desolve.classified()
,
desolve.data.frame()
,
desolve.decorated()
,
desolve.dvec()
,
resolve()
,
resolve.classified()
,
resolve.data.frame()
,
resolve.dvec()
Other interface:
canonical.decorated()
,
classified.data.frame()
,
decorate.character()
,
decorate.data.frame()
,
desolve.decorated()
,
enscript.default()
,
ggplot.decorated()
,
io_csv.character()
,
io_csv.data.frame()
,
io_res.character()
,
io_res.decorated()
,
io_table.character()
,
io_table.data.frame()
,
io_yamlet.character()
,
io_yamlet.data.frame()
,
is_parseable.default()
,
mimic.default()
,
modify.default()
,
promote.list()
,
read_yamlet()
,
selected.default()
,
write_yamlet()
# generate some decorated data library(magrittr) file <- system.file(package = 'yamlet', 'extdata','quinidine.csv') x <- decorate(file) x %>% decorations(Age, glyco) # resolve everything, and show selected decorations x %>% resolve %>% decorations(Age, glyco) # resolve selectively, and show selected decorations x %>% resolve(glyco) %>% decorations(Age, glyco)
# generate some decorated data library(magrittr) file <- system.file(package = 'yamlet', 'extdata','quinidine.csv') x <- decorate(file) x %>% decorations(Age, glyco) # resolve everything, and show selected decorations x %>% resolve %>% decorations(Age, glyco) # resolve selectively, and show selected decorations x %>% resolve(glyco) %>% decorations(Age, glyco)
Writes yamlet to file. Similar to io_yamlet.yamlet
but returns invisible storage format instead of invisible storage location.
write_yamlet( x, con = stdout(), eol = "\n", useBytes = FALSE, default_keys = getOption("yamlet_default_keys", list("label", "guide")), fileEncoding = getOption("encoding"), block = FALSE, ... )
write_yamlet( x, con = stdout(), eol = "\n", useBytes = FALSE, default_keys = getOption("yamlet_default_keys", list("label", "guide")), fileEncoding = getOption("encoding"), block = FALSE, ... )
x |
something that can be coerced to class 'yamlet', like a yamlet object or a decorated data.frame |
con |
passed to |
eol |
end-of-line; passed to |
useBytes |
passed to |
default_keys |
character: default keys for the first n anonymous members of each element |
fileEncoding |
if |
block |
whether to write block scalars |
... |
passed to |
invisible character representation of yamlet (storage syntax)
Other interface:
canonical.decorated()
,
classified.data.frame()
,
decorate.character()
,
decorate.data.frame()
,
desolve.decorated()
,
enscript.default()
,
ggplot.decorated()
,
io_csv.character()
,
io_csv.data.frame()
,
io_res.character()
,
io_res.decorated()
,
io_table.character()
,
io_table.data.frame()
,
io_yamlet.character()
,
io_yamlet.data.frame()
,
is_parseable.default()
,
mimic.default()
,
modify.default()
,
promote.list()
,
read_yamlet()
,
resolve.decorated()
,
selected.default()
library(csv) file <- system.file(package = 'yamlet', 'extdata','quinidine.csv') meta <- system.file(package = 'yamlet', 'extdata','quinidine.yaml') x <- as.csv(file) y <- read_yamlet(meta) x <- decorate(x, meta = y) identical(x, decorate(file)) tmp <- tempfile() write_yamlet(x, tmp) stopifnot(identical(read_yamlet(meta), read_yamlet(tmp)))
library(csv) file <- system.file(package = 'yamlet', 'extdata','quinidine.csv') meta <- system.file(package = 'yamlet', 'extdata','quinidine.yaml') x <- as.csv(file) y <- read_yamlet(meta) x <- decorate(x, meta = y) identical(x, decorate(file)) tmp <- tempfile() write_yamlet(x, tmp) stopifnot(identical(read_yamlet(meta), read_yamlet(tmp)))
The yamlet package supports storage and retrieval of table
metadata in yaml format. The most important function
is decorate.character
: it lets you 'decorate'
your data by attaching attributes retrieved from a file in
yaml format. Typically your data will be of class
'data.frame', but it could be anything that is essentially
a named list.
Storage format for 'yamlet' is a text file containing well-formed yaml. Technically, it is a map of sequences. Though well formed, it need not be complete: attributes or their names may be missing.
In the simplest case, the data specification consists of a list of column (item) names, followed by semicolons. Perhaps you only have one column:
mpg:
or maybe several:
mpg: cyl: disp:
If you know descriptive labels for your columns, provide them (skip a space after the colon).
mpg: fuel economy cyl: number of cylinders disp: displacement
If you know units, create a sequence with square brackets.
mpg: [ fuel economy, miles/gallon ] cyl: number of cylinders disp: [ displacement , in^3 ]
If you are going to give units, you probably should give a key first, since the first anonymous element is 'label' by default, and the second is 'guide'. (A guide can be units for numeric variables, factor levels/labels for categorical variables, or a format string for dates, times, and datetimes.) You could give just the units but you would have to be specific:
mpg: [units: miles/gallon]
You can over-ride default keys by providing them in your data:
mpg: [units: miles/gallon] _keys: [label, units]
Notice that stored yamlet can be informationally defective while syntactically correct. If you don't know an item key at the time of data authoring, you can omit it:
race: [race, [white: 0, black: 1, 2, asian: 3 ]]
Or perhaps you know the key but not the value:
race: [race, [white: 0, black: 1, asian: 2, ? other ]]
Notice that race
is factor-like; the factor sequence
is nested within the attribute sequence. Equivalently:
race: [label: race, guide: [white: 0, black: 1, asian: 2, ? other ]]
If you have a codelist of length one, you should still enclose it in brackets:
sex: [Sex, [ M ]]
To get started using yamlet, see ?as_yamlet.character
and
examples there. See also ?decorate
which adds yamlet
values to corresponding items in your data. See also ?print.decorated
which uses label attributes, if present, as axis labels.
Note: the quinidine and phenobarb datasets in the examples
are borrowed from nlme (?Quinidine
, ?Phenobarb
),
with some reorganization.
Maintainer: Tim Bergsma [email protected]
Useful links:
Report bugs at https://github.com/bergsmat/yamlet/issues
Displays global yamlet options: those options whose names begin with 'yamlet_'.
yamlet_append_units_open: see append_units.default
.
Controls how labels are constructed for variables
with 'units' attributes. In brief, units are wrapped in parentheses,
and appended to the label.
yamlet_append_units_close: see append_units.default
.
Controls how labels are constructed for variables
with 'units' attributes. In brief, units are wrapped in parentheses,
and appended to the label.
yamlet_append_units_style: see append_units.default
.
Determines parsing as 'plotmath' or 'latex', or 'plain' for no parsing.
yamlet_append_units_target: see append_units.default
.
By default, append result is assigned to attribute 'label', but could be
something else like 'title'.
yamlet_default_keys: see as_yamlet.character
.
The first two yaml attributes without specified names
are assumed to be 'label' and 'guide'.
yamlet_persistence: see decorate.list
and
as.integer.classified
. By default, persistence
of column attributes is implemented by creating 'dvec' objects
(decorated vectors) using vctrs methodology.
yamlet_cell_value: see as.data.frame.yamlet
.
Controls how cells are calculated when converting yamlet
(decorations) to a data.frame.
yamlet_import: see decorate.character
.
Controls how primary data is read from file (default: as.csv()).
yamlet_extension: see decorate.character
.
Controls what file extension is expected for yaml metadata
(default: '.yaml')
yamlet_overwrite: see decorate.list
.
Controls whether existing decorations are overwritten.
yamlet_exclude_attr: see decorations.data.frame
Controls what attributes are excluded from display.
yamlet_with_title: see make_title.dvec
and drop_title.dvec
.
For objects with (implied) units attributes, titles are by default
automatically created on resolve() and destroyed on desolve().
Interacts with yamlet_append_units_*.
yamlet_infer_guide: see explicit_guide.yamlet
.
Identifies the function that will be used to reclassify 'guide' as something
more explicit.
yamlet_explicit_guide_overwrite: see explicit_guide.data.frame
and explicit_guide.dvec
. In the latter case, controls
whether existing attributes are overwritten.
yamlet_explicit_guide_simplify: explicit_guide.data.frame
and explicit_guide.dvec
. Ordinarily, the 'guide' attribute
is removed if something more useful can be inferred.
yamlet_decorated_ggplot_search: see print.decorated_ggplot
.
The print method for decorated_ggplot populates axis labels by searching
first for attributes named 'expression', 'title', and 'label'. Customizable.
yamlet_decorated_ggplot_discrete: see print.decorated_ggplot
.
Discrete aesthetics to map from data decorations where available.
yamlet_decorated_ggplot_drop: see print.decorated_ggplot
.
Should unused factor levels be omitted from data-driven discrete scales?
yamlet_ggready_parse: see ggready.data.frame
,
ggready.decorated
. Whether to parse axis labels.
TRUE by default, but may be problematic if unintended.
yamlet_modify_reserved: see modify.default
. A list of
reserved labels that warn on reassignment.
yamlet_promote_reserved: see promote.list
.
Attributes to leave untouched when promoting singularities.
yamlet_promote: see filter.decorated
.
Whether to promote when filtering 'decorated'.
yamlet_as_units_preserve: as_units.dvec
.
What attributes to preserve when converting dvec to units.
Just 'label' by default.
Assign options(yamlet_as_units_preserve = character(0))
to remove all.
yamlet_print_simplify: print.yamlet
.
Whether to collapse interactively-displayed decorations
into a single line for lists that have no (nested) names
and have the same length when unlisted. True by default.
Can be misleading for lists with fine detail, but in
most cases fine detail will likely have names.
yamlet_format: enscript.default
.
Choice of 'html' or 'latex', guessed if not supplied.
yamlet_warn_conflicted: c.classified
.
Whether to warn when codelists for combined classified
factors have conflicting names (which will be dropped).
yamlet_expand_codelist: explicit_guide.yamlet
.
If TRUE (default) an empty list as a guide attribute
is short-hand for sort(unique(x))
.
yamlet_collapse_codelist: implicit_guide.data.frame
.
An integer (default: Inf) giving the maximum number of (un-named)
codelist elements to store explicitly. Else, if sort(unique(x))
has exactly the same values as codelist, implicit_guide
will
substitute an empty list.
yamlet_options()
yamlet_options()
list
yamlet_options()
yamlet_options()