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

imodify drops the list name for a single element list #831

Closed
ahjames11 opened this issue Jun 4, 2021 · 2 comments
Closed

imodify drops the list name for a single element list #831

ahjames11 opened this issue Jun 4, 2021 · 2 comments
Labels
bug an unexpected problem or unintended behavior modify 🛠️

Comments

@ahjames11
Copy link

For a single element list, imodify() is dropping the list name. Here is a 2 element list, which returns what I would expect.

library(purrr) # version 0.3.4
library(dplyr)

mtcars1 <- mtcars[1, 1:2]
mtcars2 <- mtcars[2, 1:2]

# multi element list
x <- lst(mtcars1, mtcars2)

# okay
imodify(x, ~ mutate(.x, mpg = log(mpg)))
#> $mtcars1
#>                mpg cyl
#> Mazda RX4 3.044522   6
#> 
#> $mtcars2
#>                    mpg cyl
#> Mazda RX4 Wag 3.044522   6

I would expect the single element list to provide the name as well. But it is unnamed ([[1]]). You do get the list name with modify().

# single element list
y <- x[1]

# unexpected
imodify(y, ~ mutate(.x, mpg = log(mpg)))
#> [[1]]
#>                mpg cyl
#> Mazda RX4 3.044522   6

# okay
modify(y, ~ mutate(.x, mpg = log(mpg)))
#> $mtcars1
#>                mpg cyl
#> Mazda RX4 3.044522   6

Created on 2021-06-04 by the reprex package (v2.0.0)

@hadley
Copy link
Member

hadley commented Aug 23, 2022

Somewhat more minimal reprex:

library(purrr)
x <- list(x = 10, y = 20)
str(imodify(x, \(x, y) y))
#> List of 2
#>  $ x: chr "x"
#>  $ y: chr "y"
str(imodify(x[1], \(x, y) x * 2))
#> List of 1
#>  $ : num 20

Created on 2022-08-23 by the reprex package (v2.0.1)

@hadley hadley added bug an unexpected problem or unintended behavior map 🗺️ modify 🛠️ and removed map 🗺️ labels Aug 23, 2022
@hadley
Copy link
Member

hadley commented Aug 27, 2022

Also part of #878

@hadley hadley closed this as completed Aug 27, 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
Development

No branches or pull requests

2 participants