Попробуйте следующее:
library(openxlsx)
library(xlsx)
library(readxl)
library(purrr)
Предположим, вы создали две книги, каждая с несколькими листами:
Я использую примеры книг из пакетов openxlsx
и xlsx
указаны пути к файлам книги. Я использовал пример тестовых файлов xlsx, на практике вы бы просто использовали your_wb <- "path_to_your_file.xlsx"
# sample workbooks
wb1 <- system.file("extdata", "readTest.xlsx", package = "openxlsx")
wb2 <- system.file("tests", "test_import.xlsx", package = "xlsx")
# vector of workbook paths
wbs <- c(wb1, wb2)
# read worksheet names from both workbooks into a list which becomes the contents of your "cloned" or "big" workbook
sh_nm <- map(wbs, excel_sheets)
# number of sheets in each workbook
sh_nr <- unlist(map(sh_nm, length))
# list of workbook names to match the number of sheets respectively for each workbook
wb_list <- rep(wbs, sh_nr)
# list of all sheet names
sh_list <- unlist(sh_nm)
# combine data from all worksheets and give each list element a unique name
sh_all <-
map2(wb_list, sh_list, ~openxlsx::read.xlsx(.x, sheet = .y)) %>%
set_names(paste(rep(c("wb1", "wb2"), sh_nr), sh_list, sep = "_"))
#save data, openxlsx automatically places each list element into a separate worksheet with the list element name as the worksheet name.
openxlsx::write.xlsx(sh_all, "wb_all.xlsx")
Создано 10 июля 2020 г. пакет REPEX (v0.3.0)