Re: [R] How can I stop strwrap removing escape characters? (and solution to the screen wrapping question from 11/6)

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Mon, 16 Jun 2008 11:20:20 +0100 (BST)

I think by 'escape characters' you mean 'whitespace characters'. There are no 'escape characters' in your string -- the escape character is \, and \n and \t are converted by the parser to characters when the string is constructed and so are not escapted in the string.

?strwrap explicitly says 'Whitespace in the input is destroyed.', which seems clear enough in your case. (What is meant by 'whitespace' is not completely clear, but space, tab (\t) and LF (\n) are always whitespace and it seems it means just those three and not FF, VT nor CR.)

On Mon, 16 Jun 2008, Toby Marthews wrote:

> After the helpful reply from Greg Snow (below), I've written a function
> printtoscreen which does the screen wrapping I need (although not in a
> very elegant way). This works fine for strings without any escape
> characters in them, e.g.
>
>> printtoscreen=function(str) {
> + str2=strwrap(str,width=getOption("width"))
> + if (length(str2)>1)
> {str2[2:length(str2)]=paste("\b\b",str2[2:length(str2)],sep="")} #to
> remove the ", "s toString puts in
> + str2=paste(str2,"\n",sep="")
> + cat(str2)
> + }
>> getOption("width")
> [1] 59
>
>> cat("Measured lengths are (",toString(1:20/100),") mm.\n")
> Measured lengths are ( 0.01, 0.02, 0.03, 0.04, 0.05, 0.06,$
>
>> printtoscreen(paste("Measured lengths are (",toString(1:20/100),")
> mm.\n",sep=""))
> Measured lengths are (0.01, 0.02, 0.03, 0.04, 0.05, 0.06,
> 0.07, 0.08, 0.09, 0.1, 0.11, 0.12, 0.13, 0.14, 0.15, 0.16,
> 0.17, 0.18, 0.19, 0.2) mm.
>>
>
> HOWEVER, if you use any escape characters in your string, it fails because
> strwrap removes all of them:
>
>>
>> cat("\n\tHello.\n")
>
> Hello.
>> printtoscreen("\n\tHello.\n")
> Hello.
>>
>
> Is there any way to prevent strwrap doing this? I haven't found any
> options for this.
>
> Thanks very much (and thanks to Greg Snow for the tip about strwrap before).
>
> Toby Marthews
>
>
> ==============================================
> Le Mer 11 juin 2008 18:00, Greg Snow a écrit :
>> You could try passing your character string to the strwrap function
> first,
>> then use cat on the result.
>>
>> --
>> Gregory (Greg) L. Snow Ph.D.
>> Statistical Data Center
>> Intermountain Healthcare
>> greg.snow_at_imail.org
>> (801) 408-8111
>>
>>
>>
>>> -----Original Message-----
>>> From: r-help-bounces_at_r-project.org
>>> [mailto:r-help-bounces_at_r-project.org] On Behalf Of Toby Marthews Sent:
> Wednesday, June 11, 2008 6:52 AM
>>> To: r-help_at_r-project.org
>>> Subject: [R] Word wrapping for character objects (WINDOWS R ONLY) Can
> anybody help me with this problem? ** ONLY WINDOWS R - PROBLEM
> DOESN'T OCCUR ON LINUX **
>>> I want to print a long character to screen:
>>>> getOption("width")
>>> [1] 60
>>>> z=(1:20)/10 #z is a vector of length between 20 and 30 (depending
> on user options) containing lengths in mm (i.e. each element is 1-5
> characters long)
>>>> str1=paste("The depths chosen are (",toString(z),") mm, and more text
> ...\n")
>>>> cat(str1)
>>> The depths chosen are ( 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1,
> 1.1, 1.2, 1.3, 1.$
>>>>
>>> The problem is that on R for Windows the string is cropped by the
> window size (hence the "$"). On R for Linux, this doesn't happen and the
> text is "word wrapped" (the default for the shell, I guess):
>>>> cat(str1)
>>> The depths chosen are ( 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1,
> 1.1, 1.2, 1.3,
>>> 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2 ) mm, and more text ...
>>>>
>>> I can't find any option for "word wrapping" in the cat command
> (fill=TRUE has no effect). I also checked the menu Edit -> GUI
> preferences..., but there doesn't seem to be a "Word Wrap" option there
> either.
>>> How do I get word wrapping like this in Windows?
>>> THANKS FOR ANY HELP!
>>> Toby Marthews
>>> Previous relevant posts:
>>> - The post from 2006 about Screen Wrapping
> (http://tolstoy.newcastle.edu.au/R/help/06/05/26673.html) which Brian
> Ripley answered was about controlling how long vectors are cropped to the
> screen. Unfortunately, the width option in options() does not
> affect character objects, so I can't use that control here.
>>> - I sent the same question to r-sig-gui_at_stat.math.ethz.ch in Oct 2007,
> but noone there could help me with it.
>>> - Try the following command on Windows R with a small window
> (getOption("width")<117) and a large window (getOption("width")>117) and
> you'll see you get extra nonexistent options in the menu:
>>> a=c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");menu(a)
> I guess this is a related problem?
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

-- 
Brian D. Ripley,                  ripley_at_stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


______________________________________________ R-help_at_r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.

Received on Mon 16 Jun 2008 - 10:24:42 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 16 Jun 2008 - 10:30:44 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive