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

Fixed quotes showing in tables() output in dev. #2565

Merged
merged 4 commits into from
Jan 15, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion R/print.data.table.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ format.data.table <- function (x, ..., justify="none") {
stop("Internal structure doesn't seem to be a list. Possibly corrupt data.table.")
}
format.item <- function(x) {
if (is.atomic(x) || inherits(x,"formula")) # FR #2591 - format.data.table issue with columns of class "formula"
if (is.null(x)) # NULL item in a list column
""
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
paste("<", class(x)[1L], ">", sep="")
Expand Down
30 changes: 9 additions & 21 deletions R/tables.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,24 @@ tables <- function(mb=TRUE, order.col="NAME", width=80,
data.table(NAME = dt_n,
NROW = nrow(DT),
NCOL = ncol(DT))
if (mb)
# mb is an option because object.size() appears to be slow.
# **TO DO: revisit**
set(info_i, , "MB",
#1048576 = 1024^2
round(as.numeric(object.size(DT))/1048576))
if (mb) set(info_i, , "MB", round(as.numeric(object.size(DT))/1024^2))
# mb is an option because object.size() appears to be slow. TO DO: revisit
set(info_i, , "COLS", list(list(names(DT))))
set(info_i, , "KEY", list(list(key(DT))))
if (index) set(info_i, , "INDICES", list(list(indices(DT))))
info_i
}))
info[ , NROW := format(sprintf("%4s", prettyNum(NROW, big.mark=",")), justify="right")] # %4s is for minimum width
info[ , NCOL := format(sprintf("%4s", prettyNum(NCOL, big.mark=",")), justify="right")]
if (mb) {
total = sum(info$MB)
info[ , MB := format(sprintf("%2s", prettyNum(MB, big.mark=",")), justify="right")]
}
if (!order.col %in% names(info)) stop("order.col='",order.col,"' not a column name of info")
info = info[base::order(info[[order.col]])] # base::order to maintain locale ordering of table names
m = as.matrix(info)
colnames(m)[2] = sprintf(paste("%",nchar(m[1,"NROW"]), "s", sep=""), "NROW")
colnames(m)[3] = sprintf(paste("%",nchar(m[1,"NCOL"]), "s", sep=""), "NCOL")
if (mb) colnames(m)[4] = sprintf(paste("%", nchar(m[1,"MB"]), "s", sep=""), "MB")
m[ , "COLS"] = substring(m[,"COLS"], 1L, width)
m[ , "KEY"] = substring(m[,"KEY"], 1L, width)
if (!silent) {
print(m, quote=FALSE, right=FALSE)
if (mb) cat("Total: ", prettyNum(as.character(total), big.mark=","), "MB\n", sep="")
# prettier printing on console
tt = copy(info)
tt[ , NROW := format(sprintf("%4s", prettyNum(NROW, big.mark=",")), justify="right")] # %4s is for minimum width
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the width argument of format.default can be used instead of calling sprintf:

format(prettyNum(NROW, big.mark=","), width = 4L, justify="right")

little test I ran to convince myself:

all(replicate(1000, {
  ns = prettyNum(sample(20000, 10), big.mark = ',')
  wid = format(sprintf('%4s', ns), justify = 'right')
  spr = format(ns, width = 4L, justify = 'right')
  identical(wid, spr)
}))
# [1] TRUE

Copy link
Member

@MichaelChirico MichaelChirico Jan 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway we use the format(prettyNum(...)) enough times here to warrant a function (commaNum? print_num?). Maybe defined within the scope of tables? Or something in utils.R? (just greped for sprintf, only used in this capacity here within tables)

tt[ , NCOL := format(sprintf("%4s", prettyNum(NCOL, big.mark=",")), justify="right")]
if (mb) tt[ , MB := format(sprintf("%2s", prettyNum(MB, big.mark=",")), justify="right")]
print(tt, class=FALSE, nrow=Inf)
if (mb) cat("Total: ", prettyNum(as.character(sum(info$MB)), big.mark=","), "MB\n", sep="")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why as.character here? prettyNum should handle the type conversion?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good spot!

}
invisible(info)
}


8 changes: 3 additions & 5 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,9 @@ xenv <- new.env()
xenv$TESTDT <- TESTDT
rm(TESTDT)
DT <- data.table(a = 1)
setnames(DT, paste(rev(LETTERS), collapse=""))
test(69.4, capture.output(tables(width = 10L)),
c(" NAME NROW NCOL MB COLS KEY ",
"[1,] \"DT\" \" 1\" \" 1\" \" 0\" \"ZYXWVUTSRQ\" \"NULL\"",
"Total: 0MB"))
test(69.4, tables(), output="NAME NROW NCOL MB COLS KEY1: DT 1 1 0 a.*Total: 0MB")
DT <- data.table(A=1:2, B=3:4, C=5:6, D=7:8, E=9:10, F=11:12, G=13:14, H=15:16, key="A,D,F,G")
test(69.5, tables(), output="NAME NROW NCOL MB COLS KEY1: DT 2 8 0 A,B,C,D,E,F,... A,D,F,G.*Total: 0MB")

nenv <- new.env()
nenv$DT <- data.table(a = 1)
Expand Down