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

modify_if() can't change a list element to NULL #746

Closed
jennybc opened this issue Feb 13, 2020 · 0 comments · Fixed by #879
Closed

modify_if() can't change a list element to NULL #746

jennybc opened this issue Feb 13, 2020 · 0 comments · Fixed by #879
Labels
bug an unexpected problem or unintended behavior modify 🛠️

Comments

@jennybc
Copy link
Member

jennybc commented Feb 13, 2020

I can't find a way to set certain list elements to NULL with modify_if(). Any approach I've tried either removes the elements (which seems weird, because I thought modify_*() was size-stable) or creates an extra layer of list-nesting. This may be related to some other issues around pluck-y assignment and modify, such as #636, #634.

library(purrr)

(x <- list("a", "b"))
#> [[1]]
#> [1] "a"
#> 
#> [[2]]
#> [1] "b"

(x_I_want <- list("a", NULL))
#> [[1]]
#> [1] "a"
#> 
#> [[2]]
#> NULL

modify_if(x, ~.x == "b", NULL)
#> Error: Can't convert NULL to function
#> Backtrace:
#>     █
#>  1. ├─purrr::modify_if(x, ~.x == "b", NULL)
#>  2. └─purrr:::modify_if.default(x, ~.x == "b", NULL)
#>  3.   ├─purrr::as_mapper(.f, ...)
#>  4.   └─purrr:::as_mapper.default(.f, ...)
#>  5.     └─rlang::as_function(.f)
#>  6.       └─rlang:::abort_coercion(x, friendly_type("function"))

modify_if(x, ~.x == "b", ~ NULL)
#> [[1]]
#> [1] "a"

modify_if(x, ~.x == "b", ~ list(NULL))
#> [[1]]
#> [1] "a"
#> 
#> [[2]]
#> [[2]][[1]]
#> NULL

modify_if(x, ~.x == "b", function(z) NULL)
#> [[1]]
#> [1] "a"

modify_if(x, ~.x == "b", function(z) list(NULL))
#> [[1]]
#> [1] "a"
#> 
#> [[2]]
#> [[2]][[1]]
#> NULL

Created on 2020-02-12 by the reprex package (v0.3.0.9001)

@lionel- lionel- added the bug an unexpected problem or unintended behavior label Jun 5, 2020
hadley added a commit that referenced this issue Aug 25, 2022
Fixes #655. Fixes #746. Fixes #753. Closes #754
hadley added a commit that referenced this issue Aug 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior modify 🛠️
Projects
None yet
3 participants