GithubHelp home page GithubHelp logo

add paste & paste0 from R 4.0.1 about backports HOT 4 CLOSED

r-lib avatar r-lib commented on August 20, 2024
add paste & paste0 from R 4.0.1

from backports.

Comments (4)

gaborcsardi avatar gaborcsardi commented on August 20, 2024 1

Great, thank you!

from backports.

mllg avatar mllg commented on August 20, 2024

TBH, I have a hard time understanding the intended behavior after reading both news and man page.

from backports.

jan-glx avatar jan-glx commented on August 20, 2024

Understandably. There has been a very lengthy discussion about how it should behave r-devel.

As previously, paste0 is just paste currying the sep argument to "":

paste0 <- function(..., collapse = NULL, recycle0 = FALSE)  paste(..., sep = "", collapse = collapse, recycle0 = recycle0)

Only difference is that it now also forwards the recycle0 argument.

paste with recycle0 = FALSE (the default) behaves as previously.

To understand the new behavior for recycle0 = TRUE it helps to think of paste as two different functions by considering the following identity:

paste <==> function(..., sep = " ", collapse = NULL, recycle0 = FALSE) 
   paste(paste(..., sep = sep, recycle0 = recycle0), collapse = collapse)

Only the inner part of paste, the one that concatenates that performs an element wise concatenation, is affected by the new recycle0 argument. Previously and now with recycle0 = FALSE, paste would not recycle zero-length arguments but replace them by rep("", max(sapply(list(...), length))). This means that paste0("Hello", character(0), "!") would (somewhat unexpectedly) yield "Hello !". With the new recycle0 = TRUE, paste recycles zero-length arguments just like e.g. + does for integers:
paste0("Hello", character(0), "!", recycle0 = TRUE) yields character(0), just like 1L+integer(0)==integer(0) or (1:3)+integer(0)==integer(0).

you could implement the new paste like this (and paste0 accordingly):

paste <- function(..., sep = " ", collapse = NULL, recycle0 = FALSE) {
  if(recycle0 && any(vapply(list(...), length, FUN.VALUE = integer(1))==0)) {
    paste(sep = sep, collapse = collapse) # for argument checking
    if (is.null(collapse)) character(0) else "" 
  } else base::paste(..., sep = sep, collapse = collapse)
}

In words: if recycle0 is set to true & there are zero-length arguments paste returns the empty string "" if collapse is a string and to the zero-length value character(0) when collapse = NULL.

See also my SO question for an application.

from backports.

gaborcsardi avatar gaborcsardi commented on August 20, 2024

This would be indeed nice to have.

from backports.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.