使用read_excel上传并自动命名所有现有的工作簿工作表(不指定工作表数)

Using read_excel to upload and automatically name all existing workbook sheets (without specifying the number of sheets)

我想改编以下来自 的答案。

# Example data
write.xlsx(mtcars, "mt cars.xlsx")
write.xlsx(mtcars, "mt car s.xlsx")

temp = list.files(pattern="*.xlsx")

make_names <- function(x) {
  gsub("\.", "_", make.names(gsub("*.xlsx$", "", x)))
}
names(temp) <- make_names(temp)

list2env(lapply(temp, read.xlsx), envir = .GlobalEnv)
#> <environment: R_GlobalEnv>

ls()
#> [1] "make_names" "mt_car_s"   "mt_cars"    "temp"

让我们假设其中一个 Excel 文件有第二个 sheet(我试图创建一个可复制的,但无法弄清楚如何用 write.xlsx).

从一个 Excel 文件加载所有 Excel sheet 的代码可以在 here 中找到(感谢 akrun)。但是,就我而言,我正在尝试上传文件夹而不是文件。

我如何结合这段代码来完成这两件事?

是否有寻找更多 sheet 的选项?

像这样的东西应该可以工作:

library(readxl)
paths <- list.files(pattern="*.xlsx")
read_all_sheets <- 
  function(path) sapply(excel_sheets(path), read_excel, path = path, USE.NAMES = TRUE, simplify = FALSE)
xl_list <- sapply(paths, read_all_sheets, USE.NAMES = TRUE, simplify = FALSE)

这是否符合您的要求?它扩展到保存为 *.xlsx 的任意数量的 excel 个文件以及其中包含的任意数量的工作表。

library(tidyverse)

mtcars1 <- mtcars
mtcars2 <- mtcars

writexl::write_xlsx(list(mtcars1,mtcars2), "mtcars list.xlsx")
writexl::write_xlsx(mtcars1, "mtcars.xlsx")

List_of_sheets <- 
  # Pick all .xlsx files in working directory
  list.files(pattern="*.xlsx") %>% 
  # Loop over each file
  purrr::map(~{
    # Extract name of file and sheets within
    Excel_name = .x
    Sheets = readxl::excel_sheets(Excel_name)
    
    List = Sheets %>% 
      # For each sheet within a single .xlsx file - 
      purrr::map(~{
        # Add a column mentioning the excel file name and the sheet name
        readxl::read_excel(Excel_name,
                           sheet = .x) %>% 
          dplyr::mutate(`Excel file name` = Excel_name,
                        `Sheet name` = .x)
      })
  }) %>% 
  purrr::flatten()

names(List_of_sheets) <- List_of_sheets %>% 
  # Name the list components with the excel file name and the sheet name
  purrr::map_chr(~{
    .x %>% 
      dplyr::mutate(`Excel file and sheet name` = str_c(`Excel file name`,`Sheet name`,sep = " ")) %>% 
      dplyr::pull(`Excel file and sheet name`) %>%
      unique
  })

List_of_sheets %>% 
  # Result
  purrr::map(~{
    .x %>% 
      dplyr::slice(1:2)
  })

输出:

$`mtcars list.xlsx Sheet1`
# A tibble: 2 × 13
    mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb `Excel file name` `Sheet name`
  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>             <chr>       
1    21     6   160   110   3.9  2.62  16.5     0     1     4     4 mtcars list.xlsx  Sheet1      
2    21     6   160   110   3.9  2.88  17.0     0     1     4     4 mtcars list.xlsx  Sheet1      

$`mtcars list.xlsx Sheet2`
# A tibble: 2 × 13
    mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb `Excel file name` `Sheet name`
  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>             <chr>       
1    21     6   160   110   3.9  2.62  16.5     0     1     4     4 mtcars list.xlsx  Sheet2      
2    21     6   160   110   3.9  2.88  17.0     0     1     4     4 mtcars list.xlsx  Sheet2      

$`mtcars.xlsx Sheet1`
# A tibble: 2 × 13
    mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb `Excel file name` `Sheet name`
  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>             <chr>       
1    21     6   160   110   3.9  2.62  16.5     0     1     4     4 mtcars.xlsx       Sheet1      
2    21     6   160   110   3.9  2.88  17.0     0     1     4     4 mtcars.xlsx       Sheet1