map_df with glimpse info

Hello,
I can't perform a simple loop that stores the column names and their types.
I mean, I just want to iterate over a list of files inside a folder.
For each file, I want to store the glimpse() information in order to compare all the datasets I have according to their types and names.
I wrote something like this:

map_df(file_list, data.table::fread, dec = ",") %>% 
  capture.output(glimpse(.), file = "column_info.txt", append = TRUE)

I use fread because the files are in CSV format.
That is, I use data.table::fread to open them and then run the glimpse() function.
I even tried:

map_df(file_list, ~ fread(.x) %>% 
                     summarise_all(list(nombre = names(.), tipo = class(.))),
       .id = "id")

But it fails. If I just run until the fread syntax, it works flawlessly.
I hope I explained the issue well.
Thanks for your help and time, community.
Have a nice weekend.

How does it fail? Do you get an error message? Do you get the file "column_info.txt" created?

When I run this code, I do get a result saved in "column_info.txt", is it not the format you expect, or some other problem?

Also, did you note that in ?glimpse, you have:

x original x is (invisibly) returned, allowing glimpse() to be used within a data pipeline.
Examples

# Note that original x is (invisibly) returned, allowing `glimpse()` to be
# used within a pipeline.
mtcars %>%
  glimpse() %>%
  select(1:3)

So you might not need the capture.output(), doing something like:

map_df(file_list, data.table::fread, dec = ",") %>% 
  glimpse() %>% 
  write_lines("column_info.txt")
1 Like

Thanks, AlexisW.
Thanks a lot.

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.