Skip to content

Instantly share code, notes, and snippets.

@alekrutkowski
Last active November 22, 2022 08:44
Show Gist options
  • Save alekrutkowski/47856f1969d1c1463661fdae8d049c53 to your computer and use it in GitHub Desktop.
Save alekrutkowski/47856f1969d1c1463661fdae8d049c53 to your computer and use it in GitHub Desktop.
Helper functions to the R package `openxlsx2`
library(openxlsx2)
library(magrittr) # for the %>% operator
# Import all sheets in the Excel file as a list of data.frames
# like in the previous package version (openxlsx)
readAllSheets <- function(xlsx_file_name, drop_empty_sheets=TRUE,
suppress_warnings=TRUE, ...)
xlsx_file_name %>%
read_sheet_names() %>%
sapply(function(x)
try(`if`(suppress_warnings,suppressWarnings,identity)
(read_xlsx(xlsx_file_name,x,...)), silent=TRUE) %>%
`if`(inherits(.,'try-error') &&
attr(.,'condition')$message=='dims are inf:-inf', # empty worksheet
`if`(!drop_empty_sheets, data.frame())
,.),
simplify=FALSE) %>%
.[!sapply(.,is.null)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment