如何将一列文件名传递给 mutate 和 return dplyr 中的列数

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

我有以下功能


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

  ncol(a)
}

我想将一个包含我的 excel 文件名列的小标题传递给此函数,并创建一个新列来告诉我 excel 电子表格中的列数.

所以我的小标题(file.list)是

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

我想要以下

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

这是我试过的

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

但我得到了错误

Error: path must be a string

然后我尝试使用 quo

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

我遇到了同样的错误。

我做错了什么?

正如@Lyngbakr 在评论中所建议的,我们可以对每个 file.list

使用 map_introwwise
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))