Search code examples
rdplyrrlang

How to pass a column of file names to mutate and return the number of columns in dplyr


I have the following function


colr <- function(file){
  a <- file %>% 
    read_xlsx() 

  ncol(a)
}

I would like to pass a tibble -- which contains a column of my excel file names to this function and make a new column which tells me the number of columns in the excel spreadsheet.

so my tibble(file.list) is

> file.list
   <chr>
   file_a.xlsx
   file_b.xlsx
   file_c.xlsx

I would like the following

> file.list       ncols
   <chr>          <int>
   file_a.xlsx      10
   file_b.xlsx      10
   file_c.xlsx       2

This is what I tried

tibble(file.list) %>% 
  mutate(ncols =  colr(file.list))

but I got the error

Error: path must be a string

then I tried using quo

tibble(file.list) %>% 
  mutate(ncols =  colr(quo(file.list)))

I ended up with the same error.

What am I doing wrong?


Solution

  • As suggested in comment by @Lyngbakr we can use map_int or rowwise for each file.list

    library(dplyr)
    library(purrr)
    
    tibble::tibble(file.list) %>% 
           mutate(ncols =  map_int(file.list, colr))
    
    
    tibble::tibble(file.list) %>% 
             rowwise() %>%
             mutate(ncols =  colr(file.list))