loop_and_display: Print output

View source: R/loop_and_display.r

loop_and_displayR Documentation

Print output

Description

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.

Usage

loop_and_display(
  .tbl,
  .vars,
  .f = pander::pandoc.table,
  sort = FALSE,
  skip_if_nothing = TRUE,
  orig_to_display = list(),
  header_lvl_top = 2
)

Arguments

.tbl

dataframe. Contains columns with names in .vars.

.vars

character vector. Names of columns in .tbl for which combinations of unique elements within each define a set of row(s) within .tbl. Each such set of row(s) is passed to the function .f.

.f

function. Takes a dataframe as input, and prints or cat's output. Default is pander::pandoc.table.

sort

logical. Whether to sort the unique elements with each looping variable. For example, if we loop on the column "V1" in the dataframe data.frame(V1 = c(x2, x1, x2, x1), V2 = ...), if we do not sort the first header related to V1 will be the element x2 and the second x1 (i.e. the order in which they appear in the column). If we sort, this will be reversed and first x1 would appear. Similarly, if V1 were a factor, then sorting would force the elements to be displayed in the order they are specified in the levels of the factor. Default is FALSE.

skip_if_nothing

logical. If TRUE, then no headings or output from .f are printed for combinations of the unique elements in .vars for which there are no corresponding entries in .tbl.

orig_to_display

named list. Used to rename headers as specified by unique levels of the variables in .tbl selected by .vars, e.g. renaming "M" to "Male". Names correspond to columns in .tbl selected by .vars. Element is either a named character vector or a function. If the element is a named character vector, then the header is used to select the element within the vector. Note that numeric elements are coerced to character. If the element is a function, then the original header is passed as the first (non-default) argument to the function and the output is then taken to be the header. Default is list().

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 2.

Value

invisible(TRUE), if successful.

Examples


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"
    )
  )
)

MiguelRodo/reportutils documentation built on April 11, 2022, 3:32 p.m.