[Rd] puzzled by cat() behaviour when argument '...' is a vector (and argument 'sep' contains "\n")

Peter Ruckdeschel peter.ruckdeschel at web.de
Wed Nov 5 23:47:21 CET 2008


Hi r-devels,

I am a bit puzzled by the behaviour of  cat() --- any help is appreciated...

At least AFAICS, cat() for vector-valued '...' argument behaves in
contradiction to what I understand from the note in the help to cat()
which reads

"
Despite its name and earlier documentation, 'sep' is a vector of
     terminators rather than separators, being output after every
     vector element (including the last).  Entries are recycled as
     needed.
"
----------------------------------------------------------------------------
reproducible example code:
----------------------------------------------------------------------------

>cat(rep("x",3), sep = ".")
x.x.x
## no "." appended!

Things get even worse if "\n" features in the 'sep' vector:

>cat(rep("x",3),sep = c(".","\n","."))
x.x
x
>
## last separator "."  gets swallowed; an non-intended line feed is
inserted

----------------------------------------------------------------------------
code causing this behaviour
----------------------------------------------------------------------------
##### "\n"

I have looked a bit into the source code
        (lines 468-630 in builtin.c in src/main)
and found out, as variable pwidth is set to 1 in line 504, i.e.;

   if (strstr(CHAR(STRING_ELT(sepr, i)), "\n")) nlsep = 1; /* ASCII */

the code in lines 622-23, i.e.;
    
  if ((pwidth != INT_MAX) || nlsep)
       Rprintf("\n");

is responsible for the newline. Is this really intended?

##### separators, not terminators

Another look shows that, contrary to what is said in the help file,
an element of vector 'sep' is /not/ printed out after each element
of the vector passed as argument '...' to cat(), "including the last"
--- confer the for-loop over the elements of '...' in lines 596-617
and the print-out of the separator

  cat_printsep(sepr, ntot);

in line 600. Once again: Is this intended?

A patch fixing my problem would be easy, though might crash
other much more important code; would you have any
proposals?

Best,
Peter

-------------------------------------------------------------------
Version:
 platform = i386-pc-mingw32
 arch = i386
 os = mingw32
 system = i386, mingw32
 status = Under development (unstable)
 major = 2
 minor = 9.0
 year = 2008
 month = 10
 day = 01
 svn rev = 46589
 language = R
 version.string = R version 2.9.0 Under development (unstable)
(2008-10-01 r46589)

Windows XP (build 2600) Service Pack 3

Locale:
LC_COLLATE=German_Germany.1252;LC_CTYPE=German_Germany.1252;LC_MONETARY=German_Germany.1252;LC_NUMERIC=C;LC_TIME=German_Germany.1252

Search Path:
 .GlobalEnv, package:stats, package:graphics, package:grDevices,
package:utils, package:datasets, package:methods, Autoloads, package:base



More information about the R-devel mailing list