View source: R/loop_and_display.r
loop_and_display | R Documentation |
Loop over combinations of elements from column in a dataframe, printing the elements as Markdown headings and outputting some sort of Markdown-output (such as a plot or a table).
The key advantage over something like using
dplyr::group_by
with dplyr::do
is that the headings are printed out without repeats.
The advantage over writing out loops by hand is that it's less typing and much easier to make changes to.
loop_and_display( .tbl, .vars, .f = pander::pandoc.table, sort = FALSE, skip_if_nothing = TRUE, orig_to_display = list(), header_lvl_top = 2 )
.tbl |
dataframe.
Contains columns with names in |
.vars |
character vector.
Names of columns in |
.f |
function.
Takes a dataframe as input, and prints or cat's output.
Default is |
sort |
logical.
Whether to sort the unique elements with each looping
variable. For example, if we loop on the column |
skip_if_nothing |
logical.
If |
orig_to_display |
named list.
Used to rename headers as specified by unique levels
of the variables in |
header_lvl_top |
integer.
The level of the header used for the
highest level looping variable.
Corresponds to the number of
hashes before a Markdown heading.
Default is |
invisible(TRUE)
, if successful.
test_tbl <- tibble::tibble( x = rep(letters[1:2], each = 3), y = purrr::map(1:2, function(i) { letters[(3:5) - as.numeric(i)] }) %>% unlist() ) loop_and_display( test_tbl, .vars = c("x", "y") ) loop_and_display( test_tbl, .vars = c("x", "y"), orig_to_display = list( "x" = c( "a" = "Letter A", "b" = "Letter B" ) ) )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.