[R] R console line-wrapping

David Winsemius dwinsemius at comcast.net
Thu Sep 17 04:28:55 CEST 2009


On Sep 16, 2009, at 9:40 PM, Nick Matzke wrote:

>
> David Winsemius wrote:
>> On Sep 15, 2009, at 5:47 PM, Nick Matzke wrote:
>>> Hi all, a quick question I couldn't find the answer to in the  
>>> usual places:
>>>
>>> Is there a way to turn off line-wrapping in the R console?  Or set  
>>> the line width-before-wrapping manually?  Currently it looks like  
>>> the console linewraps after about 70 characters, this occurs even  
>>> if I increase the window size.
>>>
>>> (I want to output some simple tables to screen for students in a  
>>> computer lab course)
>> ?options
>> --------------
>> width:
>> controls the maximum number of columns on a line used in printing  
>> vectors, matrices and arrays, and when filling by cat.
>> Columns are normally the same as characters except in CJK languages.
>> You may want to change this if you re-size the window that R is  
>> running in. Valid values are 10...10000 with default normally 80.  
>> (The limits on valid values are in file ‘Print.h’ and can be  
>> changed by re-compiling R.) Some R consoles automatically change  
>> the value when they are resized.
>> ----------end help page info---------
>>>
>>> I am running R2.9, i.e. R.app, in Mac OS X.
>
> Hi again -- thanks for the help.  However, I can't get cat or print  
> to not line wrap to the console width.  This is not a huge deal  
> though since I can make my console width pretty big.
>
> However, what is weird is the behavior I get with cat when sep=tab.   
> See below.
>
> ==============
> > getOption("width")
> [1] 400
> > freqs = rep(0.25, 25)

> > print(freqs)
> [1] 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25  
> 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25
> > print(freqs, quote=FALSE)
> [1] 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25  
> 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25
> > cat(freqs)
> 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25  
> 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25
> > cat(freqs, sep="\t")
> 0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25  
> 0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25
> > cat(freqs, sep="\t", fill=FALSE)
> 0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25  
> 0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25
> > cat(freqs, sep="\t", fill=TRUE)
> 0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25  
> 0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25
> > cat(freqs, sep="\t", fill=400)
> 0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25  
> 0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25
> >
> > # Try setting wider width
> > getOption("width")
> [1] 400
> > options(width=400)
> > print(freqs)
> [1] 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25  
> 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25
> > print(freqs, quote=FALSE)
> [1] 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25  
> 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25
> > cat(freqs)
> 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25  
> 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25
> > cat(freqs, sep="\t")
> 0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25  
> 0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25
> > cat(freqs, sep="\t", fill=FALSE)
> 0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25  
> 0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25
> > cat(freqs, sep="\t", fill=TRUE)
> 0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25  
> 0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25
> > cat(freqs, sep="\t", fill=400)
> 0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25  
> 0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25	0.25
> >
> ==============
>
>
> If the line breaks don't translate to email, basically, these  
> commands:
> print(freqs)
> print(freqs, quote=FALSE)
> cat(freqs)
>
> ...don't produce line breaks until the right edge of the R console  
> window, whereas these:
>
> cat(freqs, sep="\t")
> cat(freqs, sep="\t", fill=FALSE)
> cat(freqs, sep="\t", fill=TRUE)
> cat(freqs, sep="\t", fill=400)
>
> ...all produce line breaks after only 6 columns.
>
>
> All I really want to do is print out a formatted string each time I  
> loop through a for-loop, but without quotes, without the [1], and  
> with the printed out columns lining up despite some columns having  
> text, others numbers, and others numbers of a different size.  This  
> must be a newbie question, but I can't find anything obvious in R- 
> help.

What you want is a report generator and that is not what R offers with  
its basic output functions. I'm not sure what character got put into  
those strings because my mail client does not handle them as  
<return>'s but OpenOffice.org's text processor does wrap at 80  
characters. You may want to explore some of the packages that produce   
Latex or html.   The xtable and Sweave packages are options about  
which I am pretty much clueless. (And I just proved how clueless by  
not finding Sweave in any of the repositories, so it may be an  
external program or a mode in Emacs for all I [don't] know. xtable  
_is_ a package.)


David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list