New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pluck doesn't work with built-in functions #404

Closed
dgrtwo opened this Issue Nov 5, 2017 · 3 comments

Comments

Projects
None yet
3 participants
@dgrtwo
Copy link
Member

dgrtwo commented Nov 5, 2017

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"
@lionel-

This comment has been minimized.

Copy link
Member

lionel- commented Nov 5, 2017

Thanks, this is a duplicate of #370. We need to evaluate the accessors in the calling environment so that lexically scoped methods (such as those of the base package which aren't registered) can be found.

@lionel- lionel- closed this Nov 5, 2017

@dgrtwo

This comment has been minimized.

Copy link
Member

dgrtwo commented Nov 5, 2017

Ah! Hadn't realized it was the same issue, thanks.

@lionel-

This comment has been minimized.

Copy link
Member

lionel- commented Nov 5, 2017

I'm sorry I read your report too fast, it does look like a different issue.

@lionel- lionel- reopened this Nov 5, 2017

@hadley hadley added the bug label Feb 4, 2018

@lionel- lionel- added the help-wanted label Feb 14, 2018

@lionel- lionel- closed this in dde383d Nov 30, 2018

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