This vignette shows you how to add labels to the columns of a data set.
knitr::opts_chunk$set(comment = '.')
library(purrr) library(dplyr) library(yspec)
We'll use the examples provided in the package
data <- ys_help$data() spec <- ys_help$spec()
The data
as_tibble(data)
The spec
spec
data <- ys_add_labels(data,spec)
It isn't obvious that anything was done here
as_tibble(data)
How can you tell that the labels were added?
labs <- map(data, attr, "label") labs[1:5]
Or do this
str(data)
Ideally, we'd like to be writing in a label
entry for every column in the
data set. You can set the ys.require.label
option to TRUE
to require this
when loading the spec (an error will be generated).
But yspec has a function called ys_get_label
that will form a label for you.
Here are the rules:
label
exists for a column, it will be usedlong
is found and it is <=
40 characters, it be usedshort
will be used; reminder that short
defaults to the
column name (col
) tooLet's look at some examples:
ys_get_label(spec)[1:3]
ys_get_label(spec$NUM)
spec$NUM$label
spec$C$label
Just as an example, we can add a custom labeling function. For example, I want the label to be the column name.
Set up a function that takes the column data as the first argument
label_fun <- function(x,...) x[["col"]]
Now, pass that function into ys_add_labels
data <- ys_add_labels(data, spec, fun = label_fun)
And check the output
map(data, attr, "label")[1:5]
Recall that the yspec object is just a list. We can always map across that list and grab the label field:s
map(spec, "label")[1:5]
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.