How to view a list like table style in r

Hi my question is inspired by this topic:

but I want to use different dataset/list:

library(repurrrsive)

data(sw_films)

max_len <- max(lengths(sw_films))

df <- purrr::map_df(sw_films, ~ c(., rep('', max_len - length(.))))


df %>%
  flextable::flextable() %>%
  flextable::autofit()

but executing that line of code:

df <- purrr::map_df(sw_films, ~ c(., rep('', max_len - length(.))))

I got an error:

Error in `dplyr::bind_rows()`:
! Argument 7 must have names.
Run `rlang::last_error()` to see where the error occurred.

First of all how to check here where is an argument 7 ?
Second, how to rectify this error ?

Error persists even if I have tried to assign names:

sw_films <- setNames(sw_films, paste0("Element_", 1:7))

Thank you for your help.

df <- do.call(cbind.data.frame, c(lapply(sw_films, function(x)
               c(x, rep('', max_len - length(x)))), stringsAsFactors = FALSE))

and this errors as well with:
Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, :
arguments suggest different rows: 1, 18, 3, 8, 4, 5

Why is that ?

the difference between the example you point to and your choice of sw_films, is that the latter is more complex; it is like a list of lists of the type seen in the example.
I.e. if you just take the first element from sw_films then the approach from example works :

library(repurrrsive)

data(sw_films)

max_len <- max(lengths(sw_films[[1]]))
df <- purrr::map_df(sw_films[[1]], ~ c(., rep('', max_len - length(.))))

Therefore you would need to iterate over this iterative solution at a higher level

library(repurrrsive)

data(sw_films)

my_inner_list_unpacker <-function(x){
  max_len <- max(lengths(x))
 purrr::map_df(x, ~ c(., rep('', max_len - length(.))))
}

(df <- purrr::map_df(sw_films,my_inner_list_unpacker))

Thank you for solution, could you please answer if possible my previous questions ?
I mean, how to check where was an argument 7 and additionally that error about different numbers of rows 1, 18, 3, 8, 4, 5.
Purrr/tidyverse error messages are a bit tricky to decipher but I would like to learn how to get somewhere where I could look for solution. I tried with rlang::last_error() but it was cryptic to me as well.

The most common tool I use to debug an error in a function call is using either of debug()/debugonce()
I.e in this case the error reported comed from dplyr::bind_rows() so debug(dplyr::bind_rows) and rerun the map. You are then in a 'browser' and you can poke around to see where the error is thrown; you can see the inputs etc.

Here is a more directly illustrative example of how the dplyr bind_rows error is triggered:

> bind_rows(list(a=1,b=2),list(a=3,b=4))
# A tibble: 2 x 2
      a     b
  <dbl> <dbl>
1     1     2
2     3     4
> bind_rows(list(a=1,b=2),list(a=3))
# A tibble: 2 x 2
      a     b
  <dbl> <dbl>
1     1     2
2     3    NA
> bind_rows(list(a=1,b=2),list(a=3,4))
Error in `bind_rows()`:
! Argument 2 must have names.
Run `rlang::last_error()` to see where the error occurred.

I.e the error is of that kind
in the do.call version; its cbind.data.frame function that you could try to debug through (likely a painful task)
but again here is a somewhat illustrative example of the problem

cbind.data.frame(list(a=1:2,b=3:4))
cbind.data.frame(list(a=1:2,b=3:5)) #making b one longer than a

Thank you very much indeed for examples and detailed explanation. It means a lot.
I have watched this about debugging as well, good stuff yet a bit more advanced for me:

https://www.youtube.com/watch?v=M5n_2jmdJ_8

best regards,
Andrzej

Does the dot (.) represent here that code for example:

~c(sw_films[[1]], rep('', max_len - length(sw_films[[1]])))

I mean specifically:

sw_films[[1]]

and so on, I mean sw_films[[2]], sw_films[[3]], up to sw_films[[7]] ?

Yes, that is what it means

Thank you,
I have always trouble with figuring out what does it mean (a dot or .x) or [[i]] in for loops but very slowly getting there.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.