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

possible bug when using map_depth() and discard_at() #1161

Open
gkuljanin opened this issue Jan 16, 2025 · 0 comments
Open

possible bug when using map_depth() and discard_at() #1161

gkuljanin opened this issue Jan 16, 2025 · 0 comments

Comments

@gkuljanin
Copy link

My team is running into a possible bug in our purrr programming. We have a nested list with information at various depth levels. We manipulate the nested list to discard/keep certain information. We discovered list elements change in an undesired way when discarding list elements at certain list depths.

Consider the following nested list:

library(purrr)

nested_list <- list(
  a_1 = list(
    a_2 = list(
      a_3 = list(
        a_4_0 = matrix(c(2, 2, 3, 3), 2, 2), 
        a_4_1 = list(
          a_5 = 9
        )
      )
    )
  )
)

nested_list
#> $a_1
#> $a_1$a_2
#> $a_1$a_2$a_3
#> $a_1$a_2$a_3$a_4_0
#>      [,1] [,2]
#> [1,]    2    3
#> [2,]    2    3
#> 
#> $a_1$a_2$a_3$a_4_1
#> $a_1$a_2$a_3$a_4_1$a_5
#> [1] 9

Clearly, we see that the a_4_0 element is a matrix. Now, when we discard element a_5, we see an undesired change to a_4_0:

nested_list %>% 
  map_depth(
    .depth = 4, 
    \(ndx) {discard_at(ndx, at = "a_5")}
  )
#> $a_1
#> $a_1$a_2
#> $a_1$a_2$a_3
#> $a_1$a_2$a_3$a_4_0
#> [1] 2 2 3 3
#> 
#> $a_1$a_2$a_3$a_4_1
#> named list()

Somewhere in the discarding process, a_4_0 turns into a vector instead of remaining a matrix. We did not discover the issue at lower depths.

We can convert the vector back into a matrix, but it would be nice if that change did not happen in the first place.

Thank you for the great purrr package!

Created on 2025-01-16 with reprex v2.1.0

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

No branches or pull requests

1 participant