readxl 在传递附加参数的同时遍历工作簿中的多个工作表
readxl Iterate over multiple worksheets in a workbook while passing additional arguments
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)
我们可以使用 ~
的匿名函数调用来做到这一点
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)
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)
我们可以使用 ~
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)