[Rd] paste(character(0), collapse="", recycle0=FALSE) should be ""

brodie gaslam brod|e@g@@|@m @end|ng |rom y@hoo@com
Sat May 23 03:12:49 CEST 2020


> On Friday, May 22, 2020, 6:16:45 PM EDT, Hervé Pagès <hpages using fredhutch.org> wrote:
>
> Gabe,
>
> It's the current behavior of paste() that is a major source of bugs:
>
>   ## Add "rs" prefix to SNP ids and collapse them in a
>   ## comma-separated string.
>   collapse_snp_ids <- function(snp_ids)
>       paste("rs", snp_ids, sep="", collapse=",")
>
>   snp_groups <- list(
>     group1=c(55, 22, 200),
>     group2=integer(0),
>     group3=c(99, 550)
>   )
>
>   vapply(snp_groups, collapse_snp_ids, character(1))
>   #            group1            group2            group3
>   # "rs55,rs22,rs200"              "rs"      "rs99,rs550"
>
> This has hit me so many times!
>
> Now with 'collapse0=TRUE', we finally have the opportunity to make it do
> the right thing. Let's not miss that opportunity.
>
> Cheers,
> H.

FWIW what convinces me is consistency with other aggregating functions applied
to zero length inputs:

sum(numeric(0))
## [1] 0

>
>
> On 5/22/20 11:26, Gabriel Becker wrote:
> > I understand that this is consistent but it also strikes me as an
> > enormous 'gotcha' of a magnitude that 'we' are trying to avoid/smooth
> > over at this point in user-facing R space.
> >
> > For the record I'm not suggesting it should return something other than
> > "", and in particular I'm not arguing that any call to paste /that does
> > not return an error/ with non-NULL collapse should return a character
> > vector of length one.



More information about the R-devel mailing list