utap
utap.Rmd
Function trees
The lobstr
package provides the abstract syntax tree
function, which I like to use for creating function trees. For example,
below is the tree for select_by_class()
:
lobstr::ast(
select_by_class(
select_column_class(
col_class()
)
)
)
█─select_by_class
└─█─select_column_class └─█─col_class
This function can be demonstrated using data with some of the test
helpers in tests/testthat/helper.R
# load testthat helpers
pkgload::load_all(helpers = TRUE)
test_data <- tibble::tibble(
list_var = list(
fct_vec = fct_maker(size = 3),
ord_vec = ord_maker(size = 3),
chr_vec = chr_maker(size = 3),
log_vec = log_maker(size = 3),
int_vec = int_maker(size = 3),
dbl_vec = dbl_maker(size = 3)
),
log_var = log_maker(size = 6, missing = TRUE),
int_var = int_maker(size = 6, missing = TRUE),
dbl_var = dbl_maker(size = 6, missing = TRUE),
facet_var = facet_maker(
facet_type = "fct",
lvls = 4, missing = TRUE,
size = 6
),
bin_var = bin_maker(
bin_type = "log",
missing = TRUE, size = 6
)
)
test_data
[38;5;246m# A tibble: 6 × 6
[39m
list_var log_var int_var dbl_var facet_var bin_var
[3m
[38;5;246m<named list>
[39m
[23m
[3m
[38;5;246m<lgl>
[39m
[23m
[3m
[38;5;246m<int>
[39m
[23m
[3m
[38;5;246m<dbl>
[39m
[23m
[3m
[38;5;246m<fct>
[39m
[23m
[3m
[38;5;246m<lgl>
[39m
[23m
[38;5;250m1
[39m
[38;5;246m<fct [3]>
[39m TRUE 1 0.1 group 1 TRUE
[38;5;250m2
[39m
[38;5;246m<ord [3]>
[39m FALSE 135 3 group 2 FALSE
[38;5;250m3
[39m
[38;5;246m<chr [3]>
[39m
[31mNA
[39m 269
[31mNA
[39m group 3
[31mNA
[39m
[38;5;250m4
[39m
[38;5;246m<lgl [3]>
[39m TRUE 403 0.1 group 4 TRUE
[38;5;250m5
[39m
[38;5;246m<int [3]>
[39m FALSE
[31mNA
[39m 3
[31mNA
[39m FALSE
[38;5;250m6
[39m
[38;5;246m<dbl [3]>
[39m
[31mNA
[39m 1
[31mNA
[39m group 1
[31mNA
[39m
select_column_class() & col_class()
select_column_class()
and col_class()
are
called from within select_by_class()
, which is a wrapper
function that includes the return_tbl
argument for
returning the columns as a named vector.
lobstr::ast(
select_by_class(
select_column_class( # nested
col_class() # not defined in roxygen2
)
)
)
[38;5;214m█
[39m─
[1m
[35mselect_by_class
[39m
[22m
└─
[38;5;214m█
[39m─
[1m
[35mselect_column_class
[39m
[22m
└─
[38;5;214m█
[39m─
[1m
[35mcol_class
[39m
[22m
select_by_class(
df = test_data,
class = c("int", "dbl"),
return_tbl = TRUE
)
select_by_class(
df = test_data,
class = c("int", "dbl"),
return_tbl = FALSE
)
[38;5;246m# A tibble: 6 × 2
[39m
int_var dbl_var
[3m
[38;5;246m<int>
[39m
[23m
[3m
[38;5;246m<dbl>
[39m
[23m
[38;5;250m1
[39m 1 0.1
[38;5;250m2
[39m 135 3
[38;5;250m3
[39m 269
[31mNA
[39m
[38;5;250m4
[39m 403 0.1
[38;5;250m5
[39m
[31mNA
[39m 3
[38;5;250m6
[39m 1
[31mNA
[39m
int_var dbl_var
"int_var" "dbl_var"
pull_[type]_cols()
All of the pull_[type]_cols()
functions use the
select_by_class()
to return the initial set of column
names.
lobstr::ast(
select_by_class(
select_column_class(
col_class()
),
pull_binary_cols(),
pull_facet_cols(),
pull_cat_cols(),
pull_numeric_cols()
)
)
█─select_by_class
├─█─select_column_class
│ └─█─col_class
├─█─pull_binary_cols
├─█─pull_facet_cols
├─█─pull_cat_cols └─█─pull_numeric_cols
pull_binary_cols()
lobstr::ast(
pull_binary_cols(
check_binary_vec(
check_log_binary(),
check_int_binary(),
check_fct_binary()
),
make_binary_vec()
)
)
█─pull_binary_cols
├─█─check_binary_vec
│ ├─█─check_log_binary
│ ├─█─check_int_binary
│ └─█─check_fct_binary └─█─make_binary_vec
pull_binary_cols(df = test_data)
log_var bin_var
"log_var" "bin_var"
pull_facet_cols()
lobstr::ast(
pull_facet_cols(
check_facet_vec(
check_chr_facet(),
check_fct_facet()
),
make_facet_vec()
)
)
█─pull_facet_cols
├─█─check_facet_vec
│ ├─█─check_chr_facet
│ └─█─check_fct_facet └─█─make_facet_vec
pull_facet_cols(df = test_data)
facet_var
"facet_var"