capture_print: Capture printed console output as string

capture_printR Documentation

Capture printed console output as string

Description

Returns what print(x) would output on the console – if collapse is set to anything other than NULL, as a character scalar (i.e. a string), otherwise as a character vector of output lines.

Usage

capture_print(x, collapse = NULL)

Arguments

x

R object of which the output of print() is to be captured.

collapse

Optional string for concatenating the results. If NULL, a character vector of print lines is returned.

Details

This is a simple convenience wrapper around utils::capture.output(). Note that ANSI escape sequences (e.g. as output by the print() methods of tidyverse packages) are included in the result. To remove them, use cli::ansi_strip().

Value

A character vector if collapse = NULL, otherwise a character scalar.

Examples

mtcars |> pal::capture_print()
mtcars |> pal::capture_print(collapse = "\n") |> cat()

# to strip ANSI escape sequences, use `cli::ansi_strip()`
mtcars |> tibble::as_tibble()

mtcars |>
  tibble::as_tibble() |>
  pal::capture_print(collapse = "\n") |>
  cli::ansi_strip() |>
  cat()

salim-b/pal documentation built on Feb. 28, 2025, 6:51 p.m.