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

how to make a safely list output into a datatable #1060

Closed
jic007 opened this issue Mar 6, 2023 · 3 comments
Closed

how to make a safely list output into a datatable #1060

jic007 opened this issue Mar 6, 2023 · 3 comments

Comments

@jic007
Copy link

jic007 commented Mar 6, 2023

how to make a safely list output into a datatable
i am working with a function to copy files using fs::file_copy
The output using safely generates a list of lists containint file path error and status and it would be much easier to dignose issues if i get it in table format. Couldnt figur out how to do it

@brownj31
Copy link

brownj31 commented Apr 24, 2023

@jic007 You can do that using enframe() and hoist().

items <- list('numbers' = 1:10, 'letters' = letters)

result <- 
  purrr::map(
    items, 
    purrr::safely(sum)
  )

result |>
  tibble::enframe() |>
  tidyr::hoist(value, 'result', 'error')
#> # A tibble: 2 x 3
#>   name    result error     
#>   <chr>    <int> <list>    
#> 1 numbers     55 <NULL>    
#> 2 letters     NA <smplErrr>

Created on 2023-04-24 with reprex v2.0.2

@brownj31
Copy link

brownj31 commented Apr 24, 2023

It does bother me that unnest_wider() doesn't work here. @hadley Is this a bug?

items <- list('numbers' = 1:10, 'letters' = letters)

result <- 
  purrr::map(
    items, 
    purrr::safely(sum)
  )

result |>
  tibble::enframe() |>
  tidyr::unnest_wider(value)
#> Error in `tidyr::unnest_wider()`:
#> i In column: `value`.
#> i In row: 2.
#> Caused by error in `list_sizes()`:
#> ! `x$error` must be a vector, not a <simpleError/error/condition> object.

#> Backtrace:
#>      x
#>   1. +-tidyr::unnest_wider(tibble::enframe(result), value)
#>   2. | \-tidyr:::col_to_wide(...)
#>   3. |   +-tidyr:::with_indexed_errors(...)
#>   4. |   | \-rlang::try_fetch(...)
#>   5. |   |   \-base::withCallingHandlers(...)
#>   6. |   \-purrr::map(...)
#>   7. |     \-purrr:::map_("list", .x, .f, ..., .progress = .progress)
#>   8. |       +-purrr:::with_indexed_errors(...)
#>   9. |       | \-base::withCallingHandlers(...)
#>  10. |       +-purrr:::call_with_cleanup(...)
#>  11. |       \-tidyr (local) .f(.x[[i]], ...)
#>  12. |         \-tidyr:::elt_to_wide(...)
#>  13. |           \-vctrs::list_sizes(x)
#>  14. \-vctrs:::stop_scalar_type(`<fn>`(`<smplErrr>`), "x$error", `<env>`)
#>  15.   \-vctrs:::stop_vctrs(...)
#>  16.     \-rlang::abort(message, class = c(class, "vctrs_error"), ..., call = vctrs_error_call(call))

Created on 2023-04-24 with reprex v2.0.2

@hadley
Copy link
Member

hadley commented Jul 26, 2023

I don't think that's an unnest_wider() bug; it's just a artefact of the slightly different semantics that it has compared to hoist() (99% of the time those differences don't matter, but here they do).

@hadley hadley closed this as completed Jul 26, 2023
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

3 participants