@@ -63,28 +63,28 @@ print.data.table = function(x, topn=getOption("datatable.print.topn"),
6363 return (invisible (x ))
6464 }
6565 if (show.indices ) {
66- indices <- names(attr(x , " index" , exact = TRUE ))
66+ indices <- names(attr(x , " index" , exact = TRUE )) # Get actual index names
6767 if (length(indices )) {
68- # Get index metadata
69- index_dt <- as.data.table(attr( x , " index " ))
70- index_names <- paste0( " index " , seq_along( indices ), " : " , gsub( " __ " , " : " , indices ))
68+ # Clean index formatting
69+ cleaned_indices <- gsub( " ^__|_ " , " , " , indices ) # Remove prefix and convert _ to ,
70+ cleaned_indices <- sub( " , $ " , " " , cleaned_indices ) # Trim trailing comma
7171
72- # Format index columns to match test expectations
73- index_dt <- setnames(copy( index_dt ), index_names )
72+ # Create header string (matches key display style)
73+ header <- paste0( " Indices: " , paste( cleaned_indices , collapse = " , " ) )
7474
75- # Combine with main data
76- toprint <- cbind(toprint , index_dt )
77-
78- # Add empty column for multi-index separation
79- if (ncol(index_dt ) > 1 ) {
80- toprint <- cbind(toprint [, 1 : (ncol(toprint )- ncol(index_dt ))],
81- " " , # Empty column for visual separation
82- toprint [, (ncol(toprint )- ncol(index_dt )+ 1 ): ncol(toprint )])
75+ # Add to existing header metadata instead of toprint
76+ if (exists(" header" , envir = parent.frame(), inherits = FALSE )) {
77+ # Match data.table's existing header handling
78+ assign(" header" , c(get(" header" , envir = parent.frame()), header ),
79+ envir = parent.frame())
8380 }
84- } else {
85- show.indices <- FALSE
81+
82+ # Remove all rbind-to-toprint logic
8683 }
87- }
84+ else {
85+ show.indices <- FALSE # No indices found
86+ }
87+ }
8888 n_x = nrow(x )
8989 if ((topn * 2L + 1L )< n_x && (n_x > nrows || ! topnmiss )) {
9090 toprint = rbindlist(list (head(x , topn ), tail(x , topn )), use.names = FALSE ) # no need to match names because head and tail of same x, and #3306
0 commit comments