| encodeString {base} | R Documentation |
Encode Character Vector as for Printing
Description
encodeString escapes the strings in a character vector in thesame wayprint.default does, and optionally fits the encodedstrings within a field width.
Usage
encodeString(x, width = 0, quote = "", na.encode = TRUE, justify = c("left", "right", "centre", "none"))Arguments
x | a character vector, or an object that can be coerced to oneby |
width | integer: the minimum field width. If |
quote | character: quoting character, if any. |
na.encode | logical: should |
justify | character: partial matches are allowed. If padding tothe minimum field width is needed, how should spaces be inserted? |
Details
This escapes backslash and the control characters ‘\a’ (bell),‘\b’ (backspace), ‘\f’ (form feed),‘\n’ (line feed, aka “newline”),‘\r’ (carriage return), ‘\t’ (tab) and ‘\v’(vertical tab) as well as any non-printable characters in asingle-byte locale, which are printed in octal notation (‘\xyz’with leading zeroes).
Which characters are non-printable depends on the current locale.Windows' reporting of printable characters is unreliable, so there allother control characters are regarded as non-printable, and allcharacters with codes 32–255 as printable in a single-byte locale.Seeprint.default for how non-printable characters arehandled in multi-byte locales.
Ifquote is a single or double quote any embedded quote of thesame type is escaped. Note that justification is of the quotedstring, hence spaces are added outside the quotes.
Value
A character vector of the same length asx, with the sameattributes (including names and dimensions) but with no class set.
Marked UTF-8 encodings are preserved.
Note
The default forwidth is different fromformat.default,which does similar things for character vectors but without encodingusing escapes.
See Also
Examples
x <- "ab\bc\ndef"print(x)cat(x) # interprets escapescat(encodeString(x), "\n", sep = "") # similar to print()factor(x) # makes use of this to print the levelsx <- c("a", "ab", "abcde")encodeString(x) # width = 0: use as little as possibleencodeString(x, 2) # use two or more (left justified)encodeString(x, width = NA) # left justificationencodeString(x, width = NA, justify = "c")encodeString(x, width = NA, justify = "r")encodeString(x, width = NA, quote = "'", justify = "r")