Skip to content
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
Closed

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

dgrtwo opened this issue Nov 5, 2017 · 3 comments

Comments

@dgrtwo
Copy link
Member

@dgrtwo 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-
Copy link
Member

@lionel- 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
Copy link
Member Author

@dgrtwo dgrtwo commented Nov 5, 2017

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

@lionel-
Copy link
Member

@lionel- lionel- commented Nov 5, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants