purrr icon indicating copy to clipboard operation
purrr copied to clipboard

Why `map2()` does not work with `seq.int()` but works with `seq()`?

Open irudnyts opened this issue 2 years ago • 3 comments

I tried to use the seq.int() method instead of the seq() generics in map2(), but I receive strange errors.

The code with the generics:

library(reprex)
library(purrr)

x <- 1:3
y <- 4:6

map2(x, y, seq)
#> [[1]]
#> [1] 1 2 3 4
#> 
#> [[2]]
#> [1] 2 3 4 5
#> 
#> [[3]]
#> [1] 3 4 5 6

But when I use seq.int(), I get this (even if I specify by, and/or length.out, and/or along.with):

library(reprex)
library(purrr)

x <- 1:3
y <- 4:6

map2(x, y, seq.int)
#> Error in .f(.x[[i]], .y[[i]], ...): argument "by" is missing, with no default

At the same time, both chunks below (using mapply() and defining nesting function) work:

library(reprex)
library(purrr)

x <- 1:3
y <- 4:6

mapply(seq.int, from = x, to = y, SIMPLIFY = FALSE)
#> [[1]]
#> [1] 1 2 3 4
#> 
#> [[2]]
#> [1] 2 3 4 5
#> 
#> [[3]]
#> [1] 3 4 5 6
library(reprex)
library(purrr)

x <- 1:3
y <- 4:6

my_seq <- function(from, to) {
    seq.int(from, to)
}

map2(x, y, my_seq)
#> [[1]]
#> [1] 1 2 3 4
#> 
#> [[2]]
#> [1] 2 3 4 5
#> 
#> [[3]]
#> [1] 3 4 5 6

irudnyts avatar Sep 23 '21 13:09 irudnyts

Not sure though if I'd better ask this on Stackoverflow 🙂

irudnyts avatar Sep 23 '21 13:09 irudnyts

This is an rlang issue:

rlang::as_closure(seq.int)(1, 2)
#> Error: argument "by" is missing, with no default

lionel- avatar Sep 23 '21 13:09 lionel-

Dunno... but map2(x, y, ~ seq.int(..1, ..2)) works.

stanstrup avatar Sep 23 '21 13:09 stanstrup

I suspect there's nothing we can do here; seq.int() is a primitive function and presumably has special handing of missing values that we can't easily replicate. But I've filed an issue at https://github.com/r-lib/rlang/issues/1468 to remind us to look into it more.

hadley avatar Aug 27 '22 07:08 hadley