lenses and stype
The stype package provides lenses for
getting and settings two types of data:
stype vector (e.g. a
context, purpose or
internal_name)stype vectors within a list-like
structure (such as a data.frame or
tibble)view and set elements of a
stype vector
A lens consists of two functions: view and
set. set can be used to update parts of a
stype vector.
x <- v_binary() %>%
set(derivation_l, "some derivation") %>%
set(long_label_l, "a new label") %>%
set(study_role_l, "outcome")
x
#> <binary[0]>And the view can be used to get parts of a vector:
view(x, derivation_l)
#> [1] "some derivation"
view(x, long_label_l)
#> [1] "a new label"
view(x, study_role_l)
#> [1] "outcome"In most cases, stype also has get_*
functions that mimic view:
get_derivation(x)
#> [1] "some derivation"
get_long_label(x)
#> [1] "a new label"view and set stype vectors
within a list-like structure
Lenses for list-like structures can be used to
view and set vectors within a data.frame. In
this example, we set all continuous variables to be
covariates, binary to exposures, and count to
outcome:
df <- tibble(
x1 = v_binary(),
x2 = v_continuous(),
x3 = v_continuous(),
x4 = v_count()
)
df <- df %>%
over_map(continuous_l, function(x) set(x, study_role_l, "covariate"))%>%
over_map(binary_l, function(x) set(x, study_role_l, "exposure")) %>%
over_map(count_l, function(x) set(x, study_role_l, "outcome"))
view(df$x1, study_role_l)
#> [1] "exposure"
view(df$x2, study_role_l)
#> [1] "covariate"
view(df$x3, study_role_l)
#> [1] "covariate"
view(df$x4, study_role_l)
#> [1] "outcome"