Skip to main content

25  Printing

Every value has a default printed form, its layout: the type, then each field on its own line.

cv
<demo::Resampler@KFold>
  n_resamples: 10
  seed: 2026

25.1 The layout

A header names the type in angle brackets, with its namespace and, for an enum, its variant. A vector shows the name of its type, its length, and its first eight items, and a nested value shows its fields one level deeper:

Fit <- new_struct(
  "Fit",
  fields = list(
    n_trees = type_integer(min = 1L),
    learning_rate = type_double(),
    importance = vec(type_double(min = 0)),
    features = vec(type_character()),
    resampler = Resampler
  )
)

fit <- Fit(
  n_trees = 500L,
  learning_rate = 0.01,
  importance = c(0.31, 0.22, 0.12, 0.1, 0.08, 0.06, 0.05, 0.04, 0.02),
  features = c("age", "bmi", "sbp"),
  resampler = cv
)
fit
<demo::Fit>
  n_trees: 500
  learning_rate: 0.01
  importance: double[9] 0.31 0.22 0.12 0.1 0.08 0.06 0.05 0.04 ...
  features: character[3] "age" "bmi" "sbp"
  resampler: <demo::Resampler@KFold>
    n_resamples: 10
    seed: 2026

The name of a base type takes the color rtemislive gives its kind of column: double blue, integer cyan, character rose, factor green, logical violet, and Date and POSIXct magenta. A factor, a matrix, a table, and a sparse matrix show their shape, and no part of the layout reads a whole vector, so a value with a field of 10^8 numbers prints at once. The header of a Rust-stored value also shows the memory it holds (Rust).

r8layout() returns the layout as text, and lays out a list or a data frame the same way, each element’s type inferred from the R object:

r8layout(list(model = "GBM", auc = c(0.81, 0.84, 0.79), resampler = cv))
<list> 3 elements
  model: "GBM"
  auc: double[3] 0.81 0.84 0.79
  resampler: <demo::Resampler@KFold>
    n_resamples: 10
    seed: 2026
r8layout(iris)
<data.frame> 150 x 5
  Sepal.Length: double[150] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 ...
  Sepal.Width: double[150] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 ...
  Petal.Length: double[150] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 ...
  Petal.Width: double[150] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 ...
  Species: factor[150] setosa setosa setosa setosa setosa setosa setosa setosa ... (3 levels)

A nested list shows as list[3] with its elements one level deeper, to a depth of three, and an element without a name shows its position, [[2]]. A level shows at most 32 elements and the layout at most 128 lines, so a data frame of 5,000 columns prints 34 lines.

25.2 Output types

R8 writes text as one of four output types: plain, ansi (colors in a terminal or console), html, and markdown. format() returns plain text unless its caller names another type, so that files, snapshots, and paste() receive no markup:

format(cv)
[1] "<demo::Resampler@KFold>" "  n_resamples: 10"      
[3] "  seed: 2026"           
format(cv, output_type = "markdown")
[1] "```"                     "<demo::Resampler@KFold>"
[3] "  n_resamples: 10"       "  seed: 2026"           
[5] "```"                    
format(cv, output_type = "markdown", inline = TRUE)
[1] "`<demo::Resampler@KFold>; n_resamples: 10; seed: 2026`"

print() determines the type with r8output_type(): the output_type argument; getOption("r8.output_type"); the environment variable R8_OUTPUT_TYPE; plain when NO_COLOR is set; ansi in Positron, in RStudio with console color, and in a terminal; plain otherwise.

25.3 format

A method of format() gives a type its text, which format() returns and print() shows. It is an impl of the standard trait Display, the trait Standard traits implements in Rust style:

implement(format, Resampler) <- function(x, ...) {
  switch(
    r8variant(x),
    KFold = sprintf("%d-fold cross-validation", x$n_resamples),
    StratSub = sprintf("%d stratified subsamples", x$n_resamples),
    Bootstrap = sprintf("%d bootstrap resamples", x$n_resamples),
    stop("unsupported resampler: ", r8variant(x))
  )
}

cv
10-fold cross-validation
format(Resampler@StratSub(train_p = 0.8))
[1] "10 stratified subsamples"

A value whose type implements Display shows the impl’s text where it is nested in another value:

fit
<demo::Fit>
  n_trees: 500
  learning_rate: 0.01
  importance: double[9] 0.31 0.22 0.12 0.1 0.08 0.06 0.05 0.04 ...
  features: character[3] "age" "bmi" "sbp"
  resampler: 10-fold cross-validation

25.4 Styled text

A method marks its text with roles, which the theme styles, and names no colors, so the same text reads in every output type. r8span() marks text with a role, r8text() joins pieces on one line, and c() stacks lines:

implement(format, Resampler) <- function(x, ...) {
  r8text(
    r8span(r8variant(x), "highlight"),
    " ",
    r8span(sprintf("(%d resamples)", x$n_resamples), "summary")
  )
}

cv
KFold (10 resamples)

The roles are bracket, type_name, variant, field_name, scalar, summary, muted, highlight, strong, emphasis, user_text, plain, and the roles of base types, type_number, type_integer, type_categorical, type_string, type_boolean, type_datetime, and type_other. An r8_text object is a character vector of plain lines, so nchar() gives its width and cat() writes plain text.

25.5 print

A method of print() replaces the output of print(). It is an impl of the standard trait Print, and returns r8_text, which print() writes in the output type it determines; print() returns the value invisibly. The declaration states this in a message:

implement(print, Resampler) <- function(x, ...) {
  c(
    r8text(r8span("Resampler", "type_name")),
    r8text("  ", format(x))
  )
}
implement(print, Resampler) implements R8's `Print` trait: print() calls this function and returns its argument invisibly. format() and the `Display` trait keep their own text; implement(format, Resampler) sets it.
cv
Resampler
  KFold (10 resamples)

A method that writes with cat() instead reads r8output_type() for the type of the running print(). format() keeps the text of the format() method, as in base R for a class with methods of both:

format(cv)
[1] "KFold (10 resamples)"

25.6 Themes

R8’s theme shows brackets, variants, and highlighted text in rtemis teal, type names and strong text in bold, and summaries and muted text in gray, colors that read on light and dark backgrounds. r8theme() replaces the styles of the roles it names; options(r8.theme = ) sets it for every call, and the theme argument of format() and print() for one:

theme <- r8theme(type_name = list(color = "#0F6A66"), muted = list(italic = TRUE))
theme
<r8 theme>
bracket          #6CA3A0 (73)
type_name        #0F6A66 (23) bold
variant          #6CA3A0 (73)
field_name       default
scalar           default
summary          #808080 (244)
type_number      #2E81CC (32)
type_integer     #008C92 (30)
type_categorical #128F5A (29)
type_string      #CC566E (167)
type_boolean     #866BD8 (98)
type_datetime    #C056AC (133)
type_other       default
user_text        default
strong           bold
emphasis         italic
muted            #808080 (244) italic
highlight        #6CA3A0 (73) bold
plain            default
print(fit, theme = theme)
<demo::Fit>
  n_trees: 500
  learning_rate: 0.01
  importance: double[9] 0.31 0.22 0.12 0.1 0.08 0.06 0.05 0.04 ...
  features: character[3] "age" "bmi" "sbp"
  resampler: KFold (10 resamples)

25.7 Documents and notebooks

In R Markdown and Quarto, an HTML document receives a value’s HTML, as on this page; inline code receives its one-line markdown; PDF, Word, and GitHub Markdown receive plain text. In Jupyter, a value shows as HTML, and a tibble column of values shows each value’s type or the first line of its Display text.

HTML styles each span through its own style attribute. html_style = "classes", or options(r8.html_style = "classes"), marks spans by class only, for a page that loads R8’s stylesheet from r8css(), which switches to rtemis green and rtemislive’s light and dark colors of base types with the page’s color scheme:

format(cv, output_type = "html", html_style = "classes")
[1] "<pre class=\"r8\"><span class=\"r8-line\" data-depth=\"0\"><span class=\"r8-highlight\">KFold</span><span class=\"r8-user_text\"> </span><span class=\"r8-summary\">(10 resamples)</span></span></pre>"
cat(substr(r8css(), 1, 200))
.r8 .r8-bracket { color:var(--r8-bracket,#6CA3A0) }
.r8 .r8-type_name { font-weight:bold }
.r8 .r8-variant { color:var(--r8-variant,#6CA3A0) }
.r8 .r8-summary { color:var(--r8-summary,#808080) }
.r8 .
© 2026 E.D. Gennatas