purrr
purrr copied to clipboard
Why `map2()` does not work with `seq.int()` but works with `seq()`?
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
Not sure though if I'd better ask this on Stackoverflow 🙂
This is an rlang issue:
rlang::as_closure(seq.int)(1, 2)
#> Error: argument "by" is missing, with no default
Dunno...
but map2(x, y, ~ seq.int(..1, ..2))
works.
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.