Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to customize format of objects in 'print.data.table' #3338

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,6 @@ S3method(unique, ITime)
# which.first
# which.last

# fmt generic to support custom printers
export(fmt)
S3method(fmt, default)
9 changes: 8 additions & 1 deletion R/print.data.table.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ format.data.table <- function (x, ..., justify="none") {
else if (is.atomic(x) || inherits(x,"formula")) # FR #2591 - format.data.table issue with columns of class "formula"
paste(c(format(head(x, 6L), justify=justify, ...), if (length(x) > 6L) "..."), collapse=",") # fix for #5435 - format has to be added here...
else
paste0("<", class(x)[1L], ">")
fmt(x)
}
# FR #1091 for pretty printing of character
# TODO: maybe instead of doing "this is...", we could do "this ... test"?
Expand Down Expand Up @@ -148,3 +148,10 @@ shouldPrint = function(x) {
# as opposed to printing a blank line, for excluding col.names per PR #1483
cut_top = function(x) cat(capture.output(x)[-1L], sep = '\n')

fmt = function(x, ...) {
UseMethod("fmt")
}

fmt.default = function(x, ...) {
paste0("<", class(x)[1L], ">")
}
6 changes: 6 additions & 0 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -13309,6 +13309,12 @@ test(1979, DT[,.(a,b,if(FALSE)c)], DT[,c("a","b")])
x <- as.array(1:5)
test(1980, names(data.table(x)), "x")

# fmt() generic is used
fmt.myclass <- function(x, ...) paste0("<", class(x)[1L], ":", x$id, ">")
registerS3method("fmt", "myclass", fmt.myclass)
DT = data.table(row = 1:2, objs = list(structure(list(id = "foo"), class = "myclass"), structure(list(id = "bar"), class = "myclass")))
test(1981.1, print(DT), output = "myclass:foo")
test(1981.2, print(DT), output = "myclass:bar")

###################################
# Add new tests above this line #
Expand Down
22 changes: 22 additions & 0 deletions man/fmt.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
\name{fmt}
\alias{fmt}
\title{ Format arbitrary objects for printing in data.table }
\description{
S3 method to customize the printing of non-atomic items
in \code{\link{print.data.table}}.

The default method returns the first class of the object
(as returned by \code{\link{class}}) in angle brackets
(\dQuote{<Class>}).
}
\usage{
fmt(x, ...)
}
\arguments{
\item{x}{ Anything. }
\item{...}{ Currently ignored. }
}
\value{
A single string.
}