Skip to content

Commit 202011d

Browse files
committed
coloumn width adjusted
1 parent 7c3800f commit 202011d

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

R/print.data.table.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ print.data.table = function(x, topn=getOption("datatable.print.topn"),
139139
print_default(toprint)
140140
return(invisible(x))
141141
}
142+
if (col.names == "none")
143+
colnames(toprint) <- rep.int("", ncol(toprint))
142144
if (nrow(toprint)>20L && col.names == "auto")
143145
# repeat colnames at the bottom if over 20 rows so you don't have to scroll up to see them
144146
# option to shut this off per request of Oleg Bondar on SO, #1482
@@ -259,8 +261,8 @@ char.trunc = function(x, trunc.char = getOption("datatable.prettyprint.char")) {
259261
dt_width = function(x, nrow, class, row.names, col.names) {
260262
widths = apply(nchar(x, type='width'), 2L, max)
261263
if (class) widths = pmax(widths, 6L)
262-
if (col.names != "none") names = sapply(colnames(x), nchar, type="width") else names = 0L
263-
dt_widths = pmax(widths, names)
264+
names_widths = if (col.names == "none") rep(0L, ncol(x)) else sapply(colnames(x), nchar, type = "width")
265+
dt_widths = pmax(widths, names_widths)
264266
rownum_width = if (row.names) as.integer(ceiling(log10(nrow))+2.0) else 0L
265267
cumsum(dt_widths + 1L) + rownum_width
266268
}
@@ -292,4 +294,4 @@ trunc_cols_message = function(not_printed, abbs, class, col.names){
292294
knit_print.data.table = function(x, ...) {
293295
if (!shouldPrint(x)) return(invisible(x))
294296
NextMethod()
295-
}
297+
}

inst/tests/tests.Rraw

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21129,3 +21129,17 @@ test(2311.2, nlevels(DT$V1), 2L) # used to be 3
2112921129
# avoid translateChar*() in OpenMP threads, #6883
2113021130
DF = list(rep(iconv("\uf8", from = "UTF-8", to = "latin1"), 2e6))
2113121131
test(2312, fwrite(DF, nullfile(), encoding = "UTF-8", nThread = 2L), NULL)
21132+
21133+
# New tests for GitHub #6882 (col.names='none' column widths)
21134+
test(2313.1, {
21135+
dt <- data.table(short=1:3, verylongcolumnname=4:6)
21136+
print(dt, col.names="none")
21137+
}, output="1: 1 4\n2: 2 5\n3: 3 6")
21138+
test(2313.2, {
21139+
dt <- data.table(x=123456, y="wide_string")
21140+
print(dt, col.names="none")
21141+
}, output="1: 123456 wide_string")
21142+
test(2313.3, {
21143+
dt <- data.table(a=NA_integer_, b=NaN)
21144+
print(dt, col.names="none")
21145+
}, output="1: NA NaN")

0 commit comments

Comments
 (0)