Learn R Programming

pillar (version 1.6.0)

ctl_new_pillar: Customize your tibble subclass

Description

Gain full control over the appearance of the pillars of your tibble subclass in its body. These methods are intended for implementers of subclasses of the "tbl" class. Users will rarely need them.

Usage

ctl_new_pillar(controller, x, width, ..., title = NULL)

ctl_new_compound_pillar(controller, x, width, ..., title = NULL)

Arguments

controller

The object of class "tbl" currently printed.

x

A vector, can also be a data frame, array or matrix in ctl_new_compound_pillar()

width

The available width, can be a vector for multiple tiers

...

These dots are for future extensions and must be empty.

title

The title, derived from the name of the column in the data

Details

ctl_new_pillar() is called to construct pillars for regular (one-dimensional) vectors. The default implementation returns an object constructed with pillar(). Extend this method to tweak pillar components returned from the default implementation. Override this method to completely change the appearance of the pillars.

ctl_new_compound_pillar() is called for compound pillars: columns that are data frames, matrices or arrays. The default implementation returns a compound pillar with suitable formatting for the titles and types of the sub-pillar. Users will only rarely need to override this method if ever.

All components must be of the same height. This restriction may be levied in the future.

Implementations should return NULL if none of the data fits the available width.

Examples

Run this code
# NOT RUN {
# Create pillar objects
ctl_new_pillar(
  palmerpenguins::penguins,
  palmerpenguins::penguins$species[1:3], width = 60
)
ctl_new_pillar(
  palmerpenguins::penguins,
  palmerpenguins::penguins$bill_length_mm[1:3],
  width = 60
)

# Packed data frame
ctl_new_compound_pillar(
  tibble::tibble(),
  palmerpenguins::penguins,
  width = 60
)

# Packed matrix
ctl_new_compound_pillar(tibble::tibble(), matrix(1:6, ncol = 2), width = 60)

# Packed array
ctl_new_compound_pillar(tibble::tibble(), Titanic, width = 60)
# }
# NOT RUN {
# Customize output
lines <- function(char = "-") {
  stopifnot(nchar(char) == 1)
  structure(char, class = "lines")
}

format.lines <- function(x, width, ...) {
  paste(rep(x, width), collapse = "")
}

ctl_new_pillar.line_tbl <- function(controller, x, width, ..., title = NULL) {
  out <- NextMethod()
  new_pillar(list(
    title = out$title,
    type = out$type,
    lines = new_pillar_component(list(lines("=")), width = 1),
    data = out$data
  ))
}

vctrs::new_data_frame(
  list(a = 1:3, b = letters[1:3]),
  class = c("line_tbl", "tbl")
)
# }

Run the code above in your browser using DataLab