[R] pretty display or print for data frames ?

Leon Yee yee.leon at gmail.com
Wed Aug 12 09:11:41 CEST 2009


Patrick Connolly wrote:
> On Wed, 12-Aug-2009 at 12:38PM +0800, Leon Yee wrote:
> 
>> Hi, all
>>
>>     I have a question of adjusting the output of a data frame with many  
>> columns. By default, print() will print out several columns according to  
>> the window size, and then it scrolls down and print out left columns.  
>> How can I make it print all the columns in the same line?
>>     I found options(width=80 or 120 or whatever) will the change the  
>> behavior of print() and got the effect what I want, but using a  
>> parameter directly doesn't work: print(x, width=xxx).
>>     Can anyone help me out? thanks.
> 
> This will get it on one line.
> 
> options(width=120); print(x)
> 
> I'm not sure what your main objective is so that might be of no use.
> If it's something you do often, you could make a little function that
> takes the name of the dataframe and the width you'd like it printed.
> You could even have the function put the width back to the initial
> setting.  (Generally speaking, printing very wide gets messy, so
> there's good reason why you might just want to do that.)

Thanks, Patrick.  Printing wide is sometimes needed. options(width=120) 
just works, but I need to changed it back to 80 every time for normal 
printing. Sort of tedious.
I'm just curious why print.data.frame does not take an argument of 
width=120, just like in print.factor.

## S3 method for class 'factor':
print(x, quote = FALSE, max.levels = NULL,
       width = getOption("width"), ...)

Best wishes,
Leon




More information about the R-help mailing list