-
Notifications
You must be signed in to change notification settings - Fork 294
Closed
Labels
Description
While pluck works with base functions such as toupper, it doesn't work with functions such as as.character or is.numeric, giving a "must be a character or numeric vector" error.
As far as I can tell, the difference is whether typeof() returns "closure" (always works) or "builtin" (doesn't work).
library(purrr)
x <- list(a = "apple", n = 3, v = 1:5)
# Using a closure works
typeof(toupper)
#> [1] "closure"
pluck(x, "a")
#> [1] "apple"
pluck(x, "a", toupper)
#> [1] "APPLE"
# but using a builtin doesn't
typeof(as.character)
#> [1] "builtin"
pluck(x, "n")
#> [1] 3
pluck(x, "n", as.character)
#> Error: Index 2 must be a character or numeric vector
pluck(x, "n", is.double)
#> Error: Index 2 must be a character or numeric vector
# It would have worked if we defined it in a function
f.1 <- function(x) as.character(x)
pluck(x, "n", f.1)
#> [1] "3"