Reputation: 26258
How do I suppress :=
from printing to the console after extending data.table
into a new class and writing a custom print method?
In this example I'm extending data.table
by adding extended.data.table
to the class.
I'm also adding an attribute to one of the columns that I want to print in a specific format.
library(data.table)
## create data.table
dt_extend <- data.table(id = 1:5,
val = letters[1:5])
## create two attributes:
## - extend.data.table onto data.table
## - extended onto the 'val' column
setattr(dt_extend, 'class', c('extended.data.table', class(dt_extend)))
setattr(dt_extend[['val']], 'extended', 'print')
## Method to format the 'extended' column when printed
print.extended.data.table <- function(edt){
## find the 'extended' column
cols <- sapply(edt, function(x) names(attributes(x)))
cols <- names(which(cols == "extended"))
## more than one column can have the 'extended' attribute
edt <- edt[,
lapply(.SD, function(y) { paste0("formatted: ", y) } ),
by = setdiff(names(edt), cols),
.SDcols = cols
]
## now call data.table print
NextMethod()
}
## these all print as expected
dt_extend
dt_extend[1, .(val)]
str(dt_extend)
# Classes ‘extended.data.table’, ‘data.table’ and 'data.frame': 5 obs. of 2 variables:
# $ id : int 1 2 3 4 5
# $ val: atomic a b c d ...
# ..- attr(*, "extended")= chr "print"
# - attr(*, ".internal.selfref")=<externalptr>
Updating by-reference using :=
correctly updates the column, but it also prints to the console.
Why does this happen, and/or how do I stop it printing?
## why does this update AND print?
dt_extend[, val2 := val]
Looks like these are all related and the issues I'm describing:
There is a line inside print.data.table
that says
if (nrows <= 0L) return(invisible()) # ability to turn off printing
Where nrows
is defined in the function arguments as
nrows=getOption("datatable.print.nrows")
Which is an option
, that can be set by
options("datatable.print.nrows" = -1L)
setting this option does indeed suppress the print when using :=
. Maybe I can be clever with this and override the given "datatable.print.nrows"
option if the user has used :=
?
Upvotes: 3
Views: 562
Reputation: 1043
to stop printing you can do invisible(dt_extend[, val2 := val])
you can check this maybe https://github.com/Rdatatable/data.table/issues/1122
Upvotes: 3