[R] format backslash behaviour

John Peters John.Peters at tip.csiro.au
Tue May 11 07:07:28 CEST 1999


Prof Brian D Ripley writes:

> On Mon, 10 May 1999, John Peters wrote:
> 
> > The following seems weird:
> > 
> > > format("\pm2")
> > [1] "pm2"
> > 
> > > format("\\pm2")
> > [1] "\\\\pm2"
> > > 
> > 
> > i.e. it seems impossible to set up a x so that "format(x)" returns 
> > "\\pm2", and in turn "cat(format(x))" prints "\pm2".  ("\pm" is latex for 
> > "plus/minus").
> > 
 
> If M is your matrix,
> 
> M[] <- gsub("\\\\\\\\", "\\\\", M)
> write(M, ncol=ncol(M), file="whatever")
> 
> does the trick for me. (That turns "\\\\pm2" into "\\pm2" that write
> then prints as "\pm2". Beware: print prints it as "\\pm2".)
> 
> -- 
> Brian D. Ripley,                  ripley at stats.ox.ac.uk
-------------------------

Many thanks.  In fact I'm trying (perhaps misguidedly) to keep programs 
interoperable with Splus 3.4, and have settled on the following "format":

formattex <- function(x){
# pads  character vector x for equal nchar of elements with leading spaces
# doesn't double backslashes (but interprets \12 etc)
        zl <- nchar(x)
        bl <- "                    "
        zs <- substring(bl, 1, max(zl) - zl)
        paste(zs, x, sep = "")
}

Leading padding also suit some numerical columns better than the 
trailing padding of "format()".

----------------------------------------------------------------------------
John Peters
CSIRO National Measurement Laboratory
PO Box 218                                Phone:    +61 2 9413 7503
Lindfield NSW 2070                        Fax:      +61 2 9413 7202
Australia                                 Internet: John.Peters at tip.CSIRO.AU
----------------------------------------------------------------------------
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list