-
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(x, NULL) errs; use cases for why it shouldn't? #813
Comments
As a follow-on, I've also noticed that library(purrr)
l <- list(x = 1:3)
pluck(l, character(0))
#> Error in pluck(l, character(0)): CHAR() can only be applied to a 'CHARSXP', not a 'NULL'
pluck(l, integer(0))
#> NULL
pluck(l, NULL)
#> Error: Index 1 must be a character or numeric vector, not a list (purrr v0.3.4) |
This is unrelated to #480. But you don't actually propose what you think |
We're currently a bit inconsistent, and the first error message isn't useful. library(purrr)
pluck(NULL, NULL)
#> NULL
pluck(list(), NULL)
#> NULL
pluck(1, NULL)
#> Error in `stop_bad_type()` at purrr/R/conditions.R:71:2:
#> ! Index 1 must be a character or numeric vector, not a double vector
pluck(NULL, integer())
#> NULL
pluck(list(), integer())
#> NULL
pluck(1, integer())
#> NULL
pluck(NULL, 1:2)
#> NULL
pluck(list(), 1:2)
#> NULL
pluck(1, 1:2)
#> Error in `stop_bad_length()` at purrr/R/conditions.R:165:2:
#> ! Index 1 must have length 1, not 2 Created on 2022-08-27 by the reprex package (v2.0.1) Seems like we need to consistently validate the length of the index. |
Even when indexing `NULL`. Also correctly reports the undesired type of index. Fixes #813
Even when indexing `NULL`. Also correctly reports the undesired type of index. Fixes #813
This is tangentially-related to #480, but is manifested in a slightly different way, so I figured to open a new issue. Currently both of these throw errors:
I think this might be too harsh.
In #480, @jennybc mentions:
Workflow Example 1
One such use case (where
NULL
and/orcharacter(0)
are part of a workflow) is in Shiny apps.shiny::radioButtons
, e.g., can be configured to have no default selection, in which case the reactive value server-side isNULL
. If those radio buttons are used to pluck a dataset out of a list, the app author now needs to add additional code (admittedly, perhaps not a lot of additional code by usingshiny::req
) to check for the un-set case. Adding this additional code for some app improvement is great; needing to add additional code to prevent apluck
error feels less great, especially sincepluck
already has.default
semantics. In the app case, for example, one might want to set.default
to hard-coded example dataset for the rest of the app to use on initialization:^ That fails when
input$my_radio_buttons
isNULL
. Adding additionalreq
orisTruthy
(or equivalent) logic simply to avoid thepluck
error when.default
is present seems cumbersome.Workflow Example 2
Another workflow example is simply using
pluck
in one list to retrieve a value that is then used as a key whenpluck
-ing from another list:^ That works great when "my_type" is present in
obj1
.But if "my_type" is missing, the outer
pluck
throws an error becausepluck(obj1, "my_type")
resolves toNULL
. So we have to add conditionals to short-circuit the outerpluck
call, returning the.default
in that case and keeping the.default
in the outerpluck
in the instances where "my_type" is present inobj1
but doesn't have a corresponding value inobj2
.In both such examples, there're clearly ways around this, mostly with lots of conditionals (or wrapping pluck in
tryCatch
), but the.default
option inpluck
feels like it was introduced to handle non-existent keys, andNULL
feels like a non-existent key. Probably the same reasoning holds forcharacter(0)
(and other zero-length values passed as keys), but I can definitely see throwing errors when passed a length > 1 vector as a pluck element key. Likewise,chuck()
should throw an error when givenNULL
as a key, as it's advertised as being strict.The text was updated successfully, but these errors were encountered: