print

0th

Percentile

Print Values

print prints its argument and returns it invisibly (via invisible(x)). It is a generic function which means that new printing methods can be easily added for new classes.

Keywords
print
Usage
print(x, …)

# S3 method for factor print(x, quote = FALSE, max.levels = NULL, width = getOption("width"), …)

# S3 method for table print(x, digits = getOption("digits"), quote = FALSE, na.print = "", zero.print = "0", justify = "none", …)

# S3 method for function print(x, useSource = TRUE, …)

Arguments
x

an object used to select a method.

further arguments passed to or from other methods.

quote

logical, indicating whether or not strings should be printed with surrounding quotes.

max.levels

integer, indicating how many levels should be printed for a factor; if 0, no extra "Levels" line will be printed. The default, NULL, entails choosing max.levels such that the levels print on one line of width width.

width

only used when max.levels is NULL, see above.

digits

minimal number of significant digits, see print.default.

na.print

character string (or NULL) indicating NA values in printed output, see print.default.

zero.print

character specifying how zeros (0) should be printed; for sparse tables, using "." can produce more readable results, similar to printing sparse matrices in Matrix.

justify

character indicating if strings should left- or right-justified or left alone, passed to format.

useSource

logical indicating if internally stored source should be used for printing when present, e.g., if options(keep.source = TRUE) has been in use.

Details

The default method, print.default has its own help page. Use methods("print") to get all the methods for the print generic.

print.factor allows some customization and is used for printing ordered factors as well.

print.table for printing tables allows other customization. As of R 3.0.0, it only prints a description in case of a table with 0-extents (this can happen if a classifier has no valid data).

See noquote as an example of a class whose main purpose is a specific print method.

References

Chambers, J. M. and Hastie, T. J. (1992) Statistical Models in S. Wadsworth & Brooks/Cole.

See Also

The default method print.default, and help for the methods above; further options, noquote.

For more customizable (but cumbersome) printing, see cat, format or also write. For a simple prototypical print method, see .print.via.format in package tools.

Aliases
  • print
  • print.factor
  • print.function
  • print.listof
  • print.simple.list
  • print.Dlist
  • print.table
Examples
library(base) require(stats) ts(1:20) #-- print is the "Default function" --> print.ts(.) is called for(i in 1:3) print(1:i) ## Printing of factors attenu$station ## 117 levels -> 'max.levels' depending on width ## ordered factors: levels "l1 < l2 < .." esoph$agegp[1:12] esoph$alcgp[1:12] ## Printing of sparse (contingency) tables set.seed(521) t1 <- round(abs(rt(200, df = 1.8))) t2 <- round(abs(rt(200, df = 1.4))) table(t1, t2) # simple print(table(t1, t2), zero.print = ".") # nicer to read ## same for non-integer "table": T <- table(t2,t1) T <- T * (1+round(rlnorm(length(T)))/4) print(T, zero.print = ".") # quite nicer, print.table(T[,2:8] * 1e9, digits=3, zero.print = ".") ## still slightly inferior to Matrix::Matrix(T) for larger T ## Corner cases with empty extents: table(1, NA) # < table of extent 1 x 0 >
Documentation reproduced from package base, version 3.4.1, License: Part of R 3.4.1

Community examples

richie@datacamp.com at Jan 20, 2017 base v3.3.2

![print-jobs](http://i.giphy.com/1qmE53MF0sHAs.gif) See [`print.default()`](https://www.rdocumentation.org/packages/base/topics/print.default) for more examples, particularly using other `...` arguments that aren't mentioned in the `print()` Usage. # Basic usage Under most circumstances, variables will automatically print their contents when you type their name. You can make this explicit by calling the `print()` function. ```{r} x <- pi ^ (1:5) x print(x) ``` If a variable name is typed from within a loop or a function ("not at the top-level"), then it won't print. In this case you have to explicitly call `print()` to print. ```{r} for(month in month.abb) { month } for(month in month.abb) { print(month) } ``` # Printing `factor`s ## `quote` argument By default, factor values and levels are printed without any quotes. By passing `quote = TRUE` you can make both elements and level be wrapped in double quotes. See also [`noquote()`](https://www.rdocumentation.org/packages/base/topics/noquote), which forces the strings to be printed without quotes. ```{r} numbers <- c("one", "two", "three", "four", "five", "six") dice <- factor(sample(numbers, 20, replace = TRUE), levels = numbers) print(dice) print(dice, quote = TRUE) ``` ## `max.levels` argument By default, `print()` will display as many factor [`levels()`](https://www.rdocumentation.org/packages/base/topics/levels) as will fit on one line. If you have many levels, sometimes you may wish to see more of them. On other occasions, you may not wish to see any of the levels. `max.levels` lets you choose how many levels will be printed. ```{r} x <- factor(rnorm(20)) print(x) # print 1 line of levels print(x, max.levels = 0) # print no levels print(x, max.levels = 20) # print all levels ``` ## `width` argument The `width` argument allows an alternate specification for how many levels are displayed. Whereas `max.levels` lets the user specify how many elements of `levels(x)` are displayed, `width` lets the user specify approximately how many _characters_ of `levels(x)` are displayed ```{r} x <- factor(rnorm(20)) print(x) # print 1 line of levels print(x, width = 0) # print first level print(x, width = 200) # print 200 char of levels ``` # Printing `table`s ## `digits` argument The `digits` argument controls the minimum number of significant digits displayed for numeric inputs. See [`print.default()`](https://www.rdocumentation.org/packages/base/topics/print.default) for examples of how this argument is used with numeric vector inputs. Under most circumstances, `table` object contain counts, so this argument is unnecessary. However, it is also possible to do arithmetic with tables, leading to non-integer values. ```{r} (x <- 1.234567 * table(sample(1:5, 100, replace = TRUE))) print(x, digits = 1) print(x, digits = 3) print(x, digits = 5) ``` ## `quote` argument This works in the same way as for `factor`s. Setting `quote = TRUE` means that the counts are displayed wrapped in double quotes. ```{r} (x <- table(sample(month.abb, 100, replace = TRUE))) print(x) print(x, quote = TRUE) ``` ## `na.print` argument As with the `digits` argument, usually `table` objects contain counts, so this argument is unneeded. If you do arithmetic on the table values, it is possible for `NA`s to occur. By default, these are displayed as blank values. You may prefer them to be displaed as `"NA"` (or any other value). ```{r} (x <- table(sample(month.abb, 100, replace = TRUE))) x[x < 8] <- NA print(x) print(x, na.print = "NA") ``` ## `zero.print` argument If you have an object with lots of zero values, it is sometimes easier to see what is happening by making those zeroes less conspicuous. `zero.print` let's you control what value is displayed; `"."` is a popular choice. ```{r} m1 <- sample(month.abb, 100, replace = TRUE) m2 <- sample(month.abb, 100, replace = TRUE) (x <- table(m1, m2)) print(x) print(x, zero.print = ".") ```