[Rd] puzzled by cat() behaviour when argument '...' is a vector (and argument 'sep' contains "\n")
Duncan Murdoch
murdoch at stats.uwo.ca
Thu Nov 6 00:52:31 CET 2008
On 05/11/2008 5:47 PM, Peter Ruckdeschel wrote:
> 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.
> "
I think you're right that the documentation is incorrect. I'd prefer a
patch to the docs, rather than a change to the behaviour: cat() is so
fundamental that any changes to it would have wide ranging consequences.
If you want to study the code and draft a documentation patch, I'll
review it and possibly commit it.
Duncan Murdoch
> ----------------------------------------------------------------------------
> 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
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
More information about the R-devel
mailing list