print.default
Default Printing
print.default
is the default method of the generic
print
function which prints its argument.
- Keywords
Usage
# S3 method for default
print(x, digits = NULL, quote = TRUE,
na.print = NULL, print.gap = NULL, right = FALSE,
max = NULL, useSource = TRUE, …)
Arguments
- x
the object to be printed.
- digits
a non-null value for
digits
specifies the minimum number of significant digits to be printed in values. The default,NULL
, usesgetOption("digits")
. (For the interpretation for complex numbers seesignif
.) Non-integer values will be rounded down, and only values greater than or equal to 1 and no greater than 22 are accepted.- quote
logical, indicating whether or not strings (
character
s) should be printed with surrounding quotes.- na.print
a character string which is used to indicate
NA
values in printed output, orNULL
(see ‘Details’).- print.gap
a non-negative integer \(\le 1024\), or
NULL
(meaning 1), giving the spacing between adjacent columns in printed vectors, matrices and arrays.- right
logical, indicating whether or not strings should be right aligned. The default is left alignment.
- max
a non-null value for
max
specifies the approximate maximum number of entries to be printed. The default,NULL
, usesgetOption("max.print")
; see that help page for more details.- useSource
logical, indicating whether to use source references or copies rather than deparsing language objects. The default is to use the original source if it is available.
- …
further arguments to be passed to or from other methods. They are ignored in this function.
Details
The default for printing NA
s is to print NA
(without
quotes) unless this is a character NA
and quote =
FALSE
, when <NA> is printed.
The same number of decimal places is used throughout a vector. This
means that digits
specifies the minimum number of significant
digits to be used, and that at least one entry will be encoded with
that minimum number. However, if all the encoded elements then have
trailing zeroes, the number of decimal places is reduced until at
least one element has a non-zero final digit. Decimal points are only
included if at least one decimal place is selected.
Attributes are printed respecting their class(es), using the values of
digits
to print.default
, but using the default values
(for the methods called) of the other arguments.
Option width
controls the printing of vectors, matrices and
arrays, and option deparse.cutoff
controls the printing of
language objects such as calls and formulae.
When the methods package is attached, print
will call
show
for R objects with formal classes if called
with no optional arguments.
Large number of digits
Note that for large values of digits
, currently for
digits >= 16
, the calculation of the number of significant
digits will depend on the platform's internal (C library)
implementation of sprintf() functionality.
Single-byte locales
If a non-printable character is encountered during output, it is represented as one of the ANSI escape sequences (\a, \b, \f, \n, \r, \t, \v, \\ and \0: see Quotes), or failing that as a 3-digit octal code: for example the UK currency pound sign in the C locale (if implemented correctly) is printed as \243. Which characters are non-printable depends on the locale. (Because some versions of Windows get this wrong, all bytes with the upper bit set are regarded as printable on Windows in a single-byte locale.)
Unicode and other multi-byte locales
In all locales, the characters in the ASCII range (0x00 to 0x7f) are printed in the same way, as-is if printable, otherwise via ANSI escape sequences or 3-digit octal escapes as described for single-byte locales.
Multi-byte non-printing characters are printed as an escape sequence of the form \uxxxx or \Uxxxxxxxx (in hexadecimal). This is the internal code for the wide-character representation of the character. If this is not known to be Unicode code points, a warning is issued. The only known exceptions are certain Japanese ISO 2022 locales on commercial Unixes, which use a concatenation of the bytes: it is unlikely that R compiles on such a system.
It is possible to have a character string in a character vector that is not valid in the current locale. If a byte is encountered that is not part of a valid character it is printed in hex in the form \xab and this is repeated until the start of a valid character. (This will rapidly recover from minor errors in UTF-8.)
Multi-byte locales
In all locales, the characters in the ASCII range (0x00 to 0x7f) are printed in the same way, as-is if printable, otherwise via ANSI escape sequences or 3-digit octal escapes as described for single-byte locales.
Multi-byte non-printing characters are printed as an escape sequence of the form \uxxxx (in hexadecimal). This is the Unicode code point of the character.
It is possible to have a character string in a character vector that is not valid in the current locale. If a byte is encountered that is not part of a valid character it is printed in hex in the form \xab and this is repeated until the start of a valid character.
See Also
The generic print
, options
.
The "noquote"
class and print method.
encodeString
, which encodes a character vector the way
it would be printed.
Examples
library(base)
# NOT RUN {
pi
print(pi, digits = 16)
LETTERS[1:16]
print(LETTERS, quote = FALSE)
M <- cbind(I = 1, matrix(1:10000, ncol = 10,
dimnames = list(NULL, LETTERS[1:10])))
utils::head(M) # makes more sense than
print(M, max = 1000) # prints 90 rows and a message about omitting 910
# }
Community examples
`print.default()` is the default method for the S3 generic [`print()`](https://www.rdocumentation.org/packages/base/topics/print) function. That means that to call `print.default()`, you just call `print()` on an object that doesn't have its own `print()` method. See [`print()`](https://www.rdocumentation.org/packages/base/topics/print) for basic usage and printing other types of variable. ## `digits` argument The `digits` argument controls the minimum number of significant digits displayed for numeric inputs. ```{r} (x <- runif(5, 1, 9)) print(x, digits = 1) print(x, digits = 3) print(x, digits = 5) ``` For vectors with several orders of magnitude in range, some numbers may have more significant digits than `digits`, and scientific formatting may be employed. ```{r} (x <- rlnorm(5, sdlog = 5)) print(x, digits = 1) print(x, digits = 3) print(x, digits = 5) ``` ## `quote` argument By default, strings are printed wrapped in double quotes. This can be turned off the passing `quote = FALSE`. For comparison, see [`noquote()`](https://www.rdocumentation.org/packages/base/topics/noquote), which forces the strings to be printed without quotes. ```{r} print(month.abb) print(month.abb, quote = FALSE) print(noquote(month.abb)) ``` ## `na.print` argument If you have an object with lots of missing values, it is sometimes easier to see what is happening by making the `NA` values less conspicuous. `na.print` let's you control what value is displayed; `"."` is a popular choice. ```{r} x <- runif(20) x[x < 0.8] <- NA print(x) print(x, na.print = ".") ``` ## `print.gap` argument By default, values are printed with 1 space in between them. It can sometimes be easier to read the values if there is more space. This is particularly true if you have a character vector with strings containing spaces that is [`noquote()`](https://www.rdocumentation.org/packages/base/topics/noquote)d. ```{r} x <- c( "bacon, lettuce, and tomato", "peanut butter and jelly", "cheese and pickle" ) print(noquote(x)) print(noquote(x), print.gap = 10) ``` ## `right` argument By default, strings are printed left-aligned. Passing `right = TRUE` makes them print right-aligned. In the following example, [`with_options()`](https://www.rdocumentation.org/packages/withr/topics/with_options) is used to temporarily set a narrow print width. ```{r} withr::with_options( c(width = 40), { print(month.name) print(month.name, right = TRUE) } ) ``` ## `max` argument By default, `print()` will print the first `getOption("max.print")` elements of an object. This can be overridden by setting `max`. ```{r} x <- rpois(1e5, lambda = 10) # length(x) is 100 000 print(x, max = 10) ```