开发者

R - write.table with variable/in-memory output?

开发者 https://www.devze.com 2023-03-05 06:08 出处:网络
I\'d li开发者_如何学编程ke to use write.table except that I want the \"output\" to be a variable in-memory and not a file on disk.Basically I\'m trying to get the string representation of a data.frame

I'd li开发者_如何学编程ke to use write.table except that I want the "output" to be a variable in-memory and not a file on disk. Basically I'm trying to get the string representation of a data.frame (with column aligned, no column headers).


Use capture.output to grab the output of print:

df.mem=capture.output(print(df, row.names=FALSE))
df.mem

[1] "   A   B   C" " 500   1  92" "  12  13 754" "   8 938   3"


Instead of writing to a file, you can write to any connection.

I can do this:

x = matrix(rnorm(100), ncol = 10)

write.table(file = pipe("pbcopy"), x)

which writes the table to the clipboard.

I can then read it back in with

read.table(pipe("pbpaste"))

EDIT:

You can also round everything to a fixed number of digits then use a sep = " " in the (write.table call) like this:

x = matrix(rnorm(100), ncol = 10)

write.table(file = pipe("pbcopy"), round(x, 2), sep = "    ")

readLines(pipe("pbpaste"))

results in

 [1] "      V1    V2    V3    V4    V5    V6    V7    V8    V9   V10"
 [2] "1  -0.73  0.10 -0.47  1.02 -0.13 -0.01 -1.00 -0.19 -1.10  0.29"
 [3] "2  -0.53 -0.29  0.60  1.41  2.09 -0.49 -2.48  0.17  0.88  1.53"
 [4] "3   1.16  0.28  0.40  0.36  0.16  0.03 -0.72  0.20  1.71 -1.10"
 [5] "4   0.63 -0.85 -0.23 -0.95  0.09  0.96  0.31 -0.05  1.07 -0.09"
 [6] "5  -1.15 -0.27  0.80  0.62 -0.77 -0.54  0.18 -0.16  0.27 -0.17"
 [7] "6   1.03 -2.19  1.54 -1.36  1.00  0.35 -0.84 -0.54 -0.33 -0.92"
 [8] "7   0.12  0.49 -2.03 -0.07  0.55  0.48  0.32  0.22 -0.44 -0.56"
 [9] "8  -0.13 -0.07 -0.28 -0.49  1.56 -2.00 -0.44 -0.04 -0.26  0.77"
[10] "9  -1.23 -0.53  0.57  0.08 -0.59  0.65 -0.15  0.44 -0.67  0.00"
[11] "10 -0.60  1.07 -1.82  0.39  1.11 -1.86  0.25  0.44  0.57  0.53"


I think this works. any critiques? I'm still ramping up on 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 )

}

test

df = data.frame( A = c( 500 , 12 , 8 ) , B = c( 1 , 13 , 938 ) , C = c( 92, 754 , 3 ) )
writeLines( PrettyPrintDataFrame( df , " | " ) )
0

精彩评论

暂无评论...
验证码 换一张
取 消