@@ -63,21 +63,26 @@ 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 ))
6767 if (length(indices )) {
68- # Clean index formatting
69- cleaned_indices <- gsub(" ^__|_" , " , " , indices )
70- cleaned_indices <- sub(" , $" , " " , cleaned_indices )
71- # Create header string (matches key display style)
72- header <- paste0(" Indices: " , paste(cleaned_indices , collapse = " , " ))
73- # Add to existing header metadata instead of toprint
74- if (exists(" header" , envir = parent.frame(), inherits = FALSE )) {
75- assign(" header" , c(get(" header" , envir = parent.frame()), header ),
76- envir = parent.frame())
68+ # Get index metadata
69+ index_dt <- as.data.table(attr(x , " index" ))
70+ index_names <- paste0(" index" , seq_along(indices ), " :" , gsub(" __" , " :" , indices ))
71+
72+ # Format index columns to match test expectations
73+ index_dt <- setnames(copy(index_dt ), index_names )
74+
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 )])
7783 }
78- }
79- else {
80- show.indices <- FALSE
84+ } else {
85+ show.indices <- FALSE
8186 }
8287 }
8388 n_x = nrow(x )
0 commit comments