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

From: Gavin Simpson <gavin.simpson_at_ucl.ac.uk>
Date: Mon, 16 Jun 2008 10:56:11 +0100

On Mon, 2008-06-16 at 11:33 +0200, 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

Hi Toby,

Have you considered writeLines() as a in-built alternative to printtoscreen:

> writeLines(strwrap(paste("Measured lengths are (", paste(1:20/100,
collapse = ", "), ") mm.", sep = ""), width = 60)) 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.

The above is how I would have tackled this previously, but I see toString is something I should add my battery of useful R manipulation routines:

> writeLines(strwrap(paste("Measured lengths are (", toString(1:20 /
100), ") mm.", sep = ""), width = 60))
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.

Note you don't need the trailing \n from your example if using writeLines (argument sep to writeLines adds this by default).

> > 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.

No, not that I can see and this is as documented on ?strwrap, though consider the prefix argument of strwrap to achieve the same end result as your simple example.

> writeLines(strwrap("\n\tHello.\n", width = 60, prefix = "\n\t"))

        Hello.

Your actual use case may be more complicated than this, however, but I don't have a solution to hand if it is.

HTH G

>
> 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.

-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

______________________________________________
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:13:52 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 - 11:30:50 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