Skip to content

Instantly share code, notes, and snippets.

@bvancil
Last active May 7, 2020 00:10
Show Gist options
  • Save bvancil/19c509e1f252280edb7ce6f739ed16f0 to your computer and use it in GitHub Desktop.
Save bvancil/19c509e1f252280edb7ce6f739ed16f0 to your computer and use it in GitHub Desktop.
purrr::map includes all arguments from a builtin when using purrr::as_mapper making .f complain about missing arguments
library(purrr)
library(rlang)
# "closure"s work for .f in map()
print(typeof(seq))
print(map(c(1L, 3L), seq, to = 5L, by = 1L))
# "builtin"s do not work for .f in map()
print(typeof(seq.int))
# Generates an error:
# Error in .f(.x[[i]], ...) :
# argument "length.out" is missing, with no default
print(map(c(1L, 3L), seq.int, to = 5L, by = 1L))
# Investigation
# * `map` calls `as_mapper(.f, ...)`, which dispatches to `purrr:::as_mapper.default`
# * `as_mapper.default` calls `rlang::as_closure`
# Generates the same error:
print(as_closure(seq.int)(1L, to = 5L, by = 1L))
library(purrr)
library(rlang)
#> 
#> Attaching package: 'rlang'
#> The following objects are masked from 'package:purrr':
#> 
#>     %@%, as_function, flatten, flatten_chr, flatten_dbl, flatten_int,
#>     flatten_lgl, flatten_raw, invoke, list_along, modify, prepend,
#>     splice
# "closure"s work for .f in map()
typeof(seq)
#> [1] "closure"
map(c(1L, 3L), seq, to = 5L, by = 1L)
#> [[1]]
#> [1] 1 2 3 4 5
#> 
#> [[2]]
#> [1] 3 4 5

# "builtin"s do not work for .f in map()
typeof(seq.int)
#> [1] "builtin"
map(c(1L, 3L), seq.int, to = 5L, by = 1L)
#> Error in .f(.x[[i]], ...): argument "length.out" is missing, with no default

# Investigation
# * `map` calls `as_mapper(.f, ...)`, which dispatches to `purrr:::as_mapper.default`
# * `as_mapper.default` calls `rlang::as_closure`
# Generates the same error:
as_closure(seq.int)(1L, to = 5L, by = 1L)
#> Error in as_closure(seq.int)(1L, to = 5L, by = 1L): argument "length.out" is missing, with no default

Created on 2020-05-06 by the reprex package (v0.3.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment