Skip to content

Instantly share code, notes, and snippets.

@mattgalbraith
Last active February 1, 2023 18:37
Show Gist options
  • Save mattgalbraith/8da6023ee8971073d1c9b15e35c85698 to your computer and use it in GitHub Desktop.
Save mattgalbraith/8da6023ee8971073d1c9b15e35c85698 to your computer and use it in GitHub Desktop.
R function to export tables in XLSX format
## Excel export function
export_excel <- function(named_list, filename = "") {
wb <- openxlsx::createWorkbook()
## Loop through the list of split tables as well as their names
## and add each one as a sheet to the workbook
Map(function(data, name){
openxlsx::addWorksheet(wb, name)
openxlsx::writeData(wb, name, data)
}, named_list, names(named_list))
## Save workbook to working directory
openxlsx::saveWorkbook(wb, file = here("results", paste0(out_file_prefix, filename, ".xlsx")), overwrite = TRUE)
cat("Saved as:", here("results", paste0(out_file_prefix, filename, ".xlsx")))
} # end of function
@mattgalbraith
Copy link
Author

Need to update to make output filename more generic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment