View source: R/print.data.table.R
print.data.table | R Documentation |
print.data.table
extends the functionalities of print.data.frame
.
Key enhancements include automatic output compression of many observations and concise column-wise class
summary.
format_col
and format_list_item
generics provide flexibility for end-users to define custom printing methods for generic classes.
## S3 method for class 'data.table' print(x, topn=getOption("datatable.print.topn"), # default: 5 nrows=getOption("datatable.print.nrows"), # default: 100 class=getOption("datatable.print.class"), # default: TRUE row.names=getOption("datatable.print.rownames"), # default: TRUE col.names=getOption("datatable.print.colnames"), # default: "auto" print.keys=getOption("datatable.print.keys"), # default: TRUE trunc.cols=getOption("datatable.print.trunc.cols"), # default: FALSE quote=FALSE, timezone=FALSE, ...) format_col(x, ...) ## Default S3 method: format_col(x, ...) ## S3 method for class 'POSIXct' format_col(x, ..., timezone=FALSE) ## S3 method for class 'expression' format_col(x, ...) format_list_item(x, ...) ## Default S3 method: format_list_item(x, ...)
x |
A |
topn |
The number of rows to be printed from the beginning and end of tables with more than |
nrows |
The number of rows which will be printed before truncation is enforced. |
class |
If |
row.names |
If |
col.names |
One of three flavours for controlling the display of column names in output. |
print.keys |
If |
trunc.cols |
If |
quote |
If |
timezone |
If |
... |
Other arguments ultimately passed to |
By default, with an eye to the typically large number of observations in a data.table
, only the beginning and end of the object are displayed (specifically, head(x, topn)
and tail(x, topn)
are displayed unless nrow(x) < nrows
, in which case all rows will print).
format_col
is applied at a column level; for example, format_col.POSIXct
is used to tag the time zones of POSIXct
columns. format_list_item
is applied to the elements (rows) of list
columns; see Examples. The default format_col
method uses getS3method
to test if a format
method exists for the column, and if so uses it. Otherwise, the default format_list_item
method uses the S3 format method (if one exists) for each item of a list
column.
print.data.table
returns x
invisibly.
format_col
returns a length(x)
-size character
vector.
format_list_item
returns a length-1 character
scalar.
print.default
#output compression DT <- data.table(a = 1:1000) print(DT, nrows = 100, topn = 4) #`quote` can be used to identify whitespace DT <- data.table(blanks = c(" 12", " 34"), noblanks = c("12", "34")) print(DT, quote = TRUE) #`class` provides handy column type summaries at a glance DT <- data.table(a = vector("integer", 3), b = vector("complex", 3), c = as.IDate(paste0("2016-02-0", 1:3))) print(DT, class = TRUE) #`row.names` can be eliminated to save space DT <- data.table(a = 1:3) print(DT, row.names = FALSE) #`print.keys` can alert which columns are currently keys DT <- data.table(a=1:3, b=4:6, c=7:9, key="b,a") setindexv(DT, c("a", "b")) setindexv(DT, "a") print(DT, print.keys=TRUE) # `trunc.cols` will make it so only columns that fit in console will be printed # with a message that states the variables not shown old_width = options("width" = 40) DT <- data.table(thing_11 = vector("integer", 3), thing_21 = vector("complex", 3), thing_31 = as.IDate(paste0("2016-02-0", 1:3)), thing_41 = "aasdfasdfasdfasdfasdfasdfasdfasdfasdfasdf", thing_51 = vector("integer", 3), thing_61 = vector("complex", 3)) print(DT, trunc.cols=TRUE) options(old_width) # Formatting customization format_col.complex = function(x, ...) sprintf('(%.1f, %.1fi)', Re(x), Im(x)) x = data.table(z = c(1 + 3i, 2 - 1i, pi + 2.718i)) print(x) iris = as.data.table(iris) iris_agg = iris[ , .(reg = list(lm(Sepal.Length ~ Petal.Length))), by = Species] format_list_item.lm = function(x, ...) sprintf('<lm:%s>', format(x$call$formula)) print(iris_agg)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.