Я думаю, что это работает. есть критика? Я все еще нарастаю на R.
PrettyPrintDataFrame = function( toPrint , separator = "" , align = "left" )
{
leftAlign = FALSE
if ( align == "left" )
{
leftAlign = TRUE
}
else if ( align == "right" ) { }
else
{
stop( "align must be 'left' or 'right'" )
}
result = ""
numberOfColumns = ncol( toPrint )
columnLenghts = array( dim = numberOfColumns )
for ( i in seq( 1:numberOfColumns ) )
{
columnLenghts[ i ] = max( nchar( toPrint[ , i ] ) )
}
numberOfRows = nrow( toPrint )
for ( row in seq( 1:numberOfRows ) )
{
if ( row > 1 )
{
result = paste( result , "\n" , sep = "" )
}
for ( col in seq( 1:numberOfColumns ) )
{
value = toPrint[ row , col ]
valuePadding = paste( rep( " " , columnLenghts[ col ]- nchar( value ) ) , collapse = "" , sep = "" )
if ( leftAlign )
{
valuePadded = paste( value , valuePadding , sep = "" )
}
else
{
valuePadded = paste( valuePadding , value , sep = "" )
}
if ( col == 1 )
{
result = paste( result , valuePadded , sep = "" )
}
else
{
result = paste( result , valuePadded , sep = separator )
}
}
}
return( result )
}
тест
df = data.frame( A = c( 500 , 12 , 8 ) , B = c( 1 , 13 , 938 ) , C = c( 92, 754 , 3 ) )
writeLines( PrettyPrintDataFrame( df , " | " ) )