readxl遍历工作簿中的多个工作表,同时传递其他参数

时间:2019-04-19 03:50:31

标签: r readxl

Readxl提供了一个很好的解决方案,可以迭代多个工作表并为这些工作表命名,效果很好。

https://readxl.tidyverse.org/articles/articles/readxl-workflows.html

path <- readxl_example("datasets.xlsx")
path %>% 
 excel_sheets() %>% 
 set_names() %>% 
 map(read_excel, path = path) 

但是我需要将其他参数传递给read_excel ..例如,下面是所需的输出。

read_excel("datasets.xlsx",col_names = FALSE, skip = 1)

我尝试过的解决方案

path <- readxl_example("datasets.xlsx")
path %>% 
 excel_sheets() %>% 
 set_names() %>% 
 map(read_excel(col_names = FALSE, skip = 1), path = path) 

read_excel_func <- function(y){
 readxl::read_excel(y, col_names = FALSE, skip = 1)
}

path <- readxl_example("datasets.xlsx")
path %>% 
 excel_sheets() %>% 
 set_names() %>% 
 map(read_excel_func, path = path)

1 个答案:

答案 0 :(得分:4)

我们可以使用带有~的匿名函数调用来完成此操作

library(readxl)
library(dplyr)
path %>% 
 excel_sheets() %>% 
   set_names() %>% 
   map(~ read_excel(.x, col_names = FALSE, skip = 1, path = path))

或者只是将map中的参数传递为path

path %>% 
  excel_sheets() %>% 
  set_names() %>% 
  map(read_excel, col_names = FALSE, skip = 1, path = path)