init_text_builder: Init a 'text_builder' object, used for incrementally adding...

View source: R/text_builder.R

init_text_builderR Documentation

Init a text_builder object, used for incrementally adding text parts

Description

This function is very useful, when you want to incrementally add text parts to a string. The text_builder mechanism uses two separate text stacks in order to store added text parts:

  • A ⁠downward stack⁠: For this stack all new text parts will be added below the previously added text parts. With add_text() new text parts can be added to this text stack.

  • An ⁠upward stack⁠: This stack uses the inverse direction, therefore all new text parts will be added above the previously added text parts. With add_text_end() new text parts can be added to the ⁠upward stack⁠. When calling create_text() the ⁠downward stack⁠ is concatenated above the ⁠upward stack⁠.

The function init_text_builder() returns a list object holding the following functions

  • add_text(text, indent_level = 0): This function adds a text part to the text stack, holding all text parts you have added so far. It takes two arguments:

    • text: A character vector holding the text parts you want to add.

    • indent_level: A non-negative integer, which defines how many times you want to add the indentation string (defined in indent) in front of the added text part.

  • add_text_end(text, indent_level = 0): This function is similar to add_text(), but adds the text part to the ⁠upward stack⁠. It takes two arguments:

    • text: A character vector holding the text parts you want to add. Be careful, although the ⁠upward stack⁠ has reversed direction, the string entries in the character vector text are not reversed. This means that for add_text_end(c("bla", "blub")) the string "bla" comes also in the finally builded text before "blub".

    • indent_level: A non-negative integer, which defines how many times you want to add the indentation string (defined in indent) in front of the added text part.

  • create_text(): This function returns the constructed text.

Usage

init_text_builder(sep = "\n", indent = "\t", before = NULL, after = NULL)

Arguments

sep

A string, which will automatically be inserted between two added text parts If you want to use no separator symbol at all, then just set sep = "".

indent

A string, used as indentation symbol. This symbol will automatically be inserted zero, one or multiple times in front of an added text part, depending on the used indent_level (can be ⁠NULL, 0, 1, 2, ...⁠) when calling add_line("SOME TEXT", indent_level = X). If you do not want to use any indentation symbols, then just set indent = NULL.

before

A string, which will automatically be added right in front of each text part.

after

A string, which will automatically be added right after each text part.

Value

A list object holding the function add_text() and get_text()


R-package/styledTables documentation built on Feb. 6, 2024, 2:21 a.m.