-
Notifications
You must be signed in to change notification settings - Fork 272
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() converts zero-length objects to NULL #480
Comments
I think this a bug in the C code called by list(list(result=data.frame(),error=1)) %>% map("result") Your 0 row, 0 column data frame has length 0 here and therefore gets the default value of Line 158 in 84ce1ad
All your other approaches use a different method to extract the element named library(purrr)
list(list(result=data.frame(1),error=1)) %>% map("result")
#> [[1]]
#> X1
#> 1 1
list(list(result=data.frame(X1 = character()),error=1)) %>% map("result")
#> [[1]]
#> [1] X1
#> <0 rows> (or 0-length row.names)
as_mapper("result")
#> function (x, ...)
#> pluck(x, list("result"), .default = NULL)
#> <environment: 0x7fc673890298> Created on 2018-03-27 by the reprex package (v0.2.0). |
I'm not sure whether this is a bug or not - |
Ok yes maybe 'bug" is wrong word. I think the surprise here comes from the fact that a user probably doesn't contemplate whether I really hadn't internalized this before: library(purrr)
x <- list(result = character())
pluck(x, list("result"))
#> NULL
x[["result"]]
#> character(0) Created on 2018-03-28 by the reprex package (v0.2.0). |
Yeah, I think we need better pluck docs and then make it more clear that's what |
It does seem sort of troubling that |
Hi, This may not be a bug but in my understanding For me Regards |
@VincentGuyader |
Ok, now I get it: library(purrr)
x <- list(
list(x = letters[1:3]),
list(x = character()),
list(x = "z")
)
map(x, "x")
map(x, list("x", 1)) The second result should be |
Most minimal reprex: purrr::pluck(list(numeric()), 1)
#> NULL This should return |
The reason for treating empty as absent is for the atomic map variants: test_that(".default replaces elements with length 0", {
x <- list(
list(a = 1),
list(a = NULL),
list(a = numeric())
)
expect_equal(map_dbl(x, "a", .default = NA), c(1, NA, NA))
}) It makes sense to treat both I wonder if this should just be an option to Maybe the cleanest way to solve this is to also take |
In other words, emptyness is absence in atomic context, but not in recursive context. |
So it's probably somehow related to r-lib/vctrs#141 |
If it matters, I think this is the same issue that I raised on the RStudio community: https://community.rstudio.com/t/possibly-inconsistent-treatment-of-extractor-in-purr-map/35093. Another example (and feeble attempts at understanding) there. |
I do think zero-length vectors and |
HI,
Hello I noticed something that seems strange to me. Dont know if it's a bug, but I dont understand the result.
map("result")
return somthing wrong (to me), butmap(~.x$result)
is OKthis is a minimal example :
Any idea why ?
Regards
The text was updated successfully, but these errors were encountered: