The R Project SVN R

Rev

Rev 85065 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42333 ripley 1
% File src/library/base/man/encodeString.Rd
68948 ripley 2
% Part of the R package, https://www.R-project.org
81602 maechler 3
% Copyright 1995-2022 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
32237 ripley 6
\name{encodeString}
56186 murdoch 7
\alias{encodeString}
32245 ripley 8
\title{
9
  Encode Character Vector as for Printing
10
}
32237 ripley 11
\description{
12
  \code{encodeString} escapes the strings in a character vector in the
13
  same way \code{print.default} does, and optionally fits the encoded
32245 ripley 14
  strings within a field width.
32237 ripley 15
}
16
\usage{
35281 ripley 17
encodeString(x, width = 0, quote = "", na.encode = TRUE,
18
             justify = c("left", "right", "centre", "none"))
32237 ripley 19
}
20
\arguments{
85065 smeyer 21
  \item{x}{a character vector, or an object that can be coerced to one
32240 ripley 22
    by \code{\link{as.character}}.}
35281 ripley 23
  \item{width}{integer: the minimum field width.  If \code{NULL} or
24
    \code{NA}, this is taken to be the largest field width needed for
25
    any element of \code{x}.}
32237 ripley 26
  \item{quote}{character: quoting character, if any.}
35281 ripley 27
  \item{na.encode}{logical: should \code{NA} strings be encoded?}
32245 ripley 28
  \item{justify}{character: partial matches are allowed.  If padding to
35281 ripley 29
    the minimum field width is needed, how should spaces be inserted?
30
    \code{justify == "none"} is equivalent to \code{width = 0}, for
31
    consistency with \code{format.default}.}
32237 ripley 32
}
33
\details{
46956 ripley 34
  This escapes backslash and the control characters \samp{\\a} (bell),
85925 hornik 35
  \samp{\\b} (backspace), \samp{\\f} (form feed),
81602 maechler 36
  \samp{\\n} (line feed, aka \dQuote{newline}),
50567 ripley 37
  \samp{\\r} (carriage return), \samp{\\t} (tab) and \samp{\\v}
38
  (vertical tab) as well as any non-printable characters in a
39
  single-byte locale, which are printed in octal notation (\samp{\\xyz}
40
  with leading zeroes).
41
 
42
  Which characters are non-printable depends on the current locale.
43
  Windows' reporting of printable characters is unreliable, so there all
32245 ripley 44
  other control characters are regarded as non-printable, and all
50567 ripley 45
  characters with codes 32--255 as printable in a single-byte locale.
35281 ripley 46
  See \code{\link{print.default}} for how non-printable characters are
47
  handled in multi-byte locales.
32237 ripley 48
 
49
  If \code{quote} is a single or double quote any embedded quote of the
32245 ripley 50
  same type is escaped.  Note that justification is of the quoted
51
  string, hence spaces are added outside the quotes.
32237 ripley 52
}
35281 ripley 53
\note{
54
  The default for \code{width} is different from \code{format.default},
38402 ripley 55
  which does similar things for character vectors but without encoding
56
  using escapes.
35281 ripley 57
}
32237 ripley 58
\value{
32245 ripley 59
  A character vector of the same length as \code{x}, with the same
35297 ripley 60
  attributes (including names and dimensions) but with no class set.
61240 ripley 61
 
71883 ripley 62
  Marked UTF-8 encodings are preserved.
32237 ripley 63
}
64
\seealso{
65
  \code{\link{print.default}}
66
}
67
\examples{
68
x <- "ab\bc\ndef"
69
print(x)
70
cat(x) # interprets escapes
61150 ripley 71
cat(encodeString(x), "\n", sep = "") # similar to print()
32237 ripley 72
 
73
factor(x) # makes use of this to print the levels
32245 ripley 74
 
75
x <- c("a", "ab", "abcde")
69952 maechler 76
encodeString(x) # width = 0: use as little as possible
77
encodeString(x, 2) # use two or more (left justified)
41593 ripley 78
encodeString(x, width = NA) # left justification
79
encodeString(x, width = NA, justify = "c")
80
encodeString(x, width = NA, justify = "r")
81
encodeString(x, width = NA, quote = "'", justify = "r")
32237 ripley 82
}
83
\keyword{ utilities }