openxlsx
openxlsx copied to clipboard
write.xlsx() does not work properly with S3 class vctrs_list_of
Describe the bug
When passing a list of dataframes with S3 class vctrs_list_of (e.g., as returned by dplyr::group_split()) to write.xlsx, the resulting Excel file has an unexpected structure. Instead of containing one sheet per list element, each list element ends up in a single cell with all columns concatenated. Only after calling as.list() on the list of class vctrs_list_of, the expected behavior is restored.
To Reproduce
library(dplyr)
library(openxlsx)
data(iris)
iris_list <- iris %>%
group_split(Species)
tmp_file <- temp_xlsx()
# Unexpected
write.xlsx(iris_list, tmp_file)
# Can be fixed by calling as.list() on vctrs_list_of
iris_list2 <- iris %>%
group_split(Species) %>%
as.list()
tmp_file2 <- temp_xlsx()
# Expected
write.xlsx(iris_list2, tmp_file2)
Expected behavior Excel file is expected to contain one sheet per list element with the columns as the dataframes in the list.
Example files unexpected.xlsx expected.xlsx
Additional context
sessionInfo()
R version 4.2.2 (2022-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.utf8 LC_CTYPE=English_United States.utf8 LC_MONETARY=English_United States.utf8 LC_NUMERIC=C LC_TIME=English_United States.utf8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] openxlsx_4.2.5.1 dplyr_1.1.4
loaded via a namespace (and not attached):
[1] fs_1.6.0 lubridate_1.9.1 insight_0.19.0 webshot_0.5.4 httr_1.4.7 R.cache_0.16.0 tools_4.2.2 backports_1.4.1 utf8_1.2.2 R6_2.5.1 sjlabelled_1.2.0
[12] lazyeval_0.2.2 colorspace_2.1-0 withr_2.5.2 tidyselect_1.2.0 gridExtra_2.3 processx_3.8.4 compiler_4.2.2 cli_3.6.0 binom_1.1-1.1 plotly_4.10.3 macRtools_1.1.2
[23] scales_1.3.0 survMisc_0.5.6 readr_2.1.3 callr_3.7.3 stringr_1.5.1 digest_0.6.35 R.utils_2.12.3 rmarkdown_2.25 pkgconfig_2.0.3 htmltools_0.5.7 styler_1.10.3
[34] labelled_2.10.0 fastmap_1.1.1 import_1.3.0 htmlwidgets_1.6.3 rlang_1.1.1 rstudioapi_0.14 generics_0.1.3 zoo_1.8-11 jsonlite_1.8.7 R.oo_1.26.0 zip_2.2.2
[45] car_3.1-1 magrittr_2.0.3 huxtable_5.5.2 Matrix_1.5-1 Rcpp_1.0.10 munsell_0.5.0 fansi_1.0.4 abind_1.4-5 clipr_0.8.0 viridis_0.6.2 R.methodsS3_1.8.2
[56] lifecycle_1.0.4 stringi_1.7.12 yaml_2.3.7 carData_3.0-5 grid_4.2.2 promises_1.2.0.1 forcats_1.0.0 crayon_1.5.2 survminer_0.4.9 lattice_0.20-45 haven_2.5.1
[67] splines_4.2.2 chromote_0.1.1 hms_1.1.2 knitr_1.46 ps_1.7.6 pillar_1.9.0 ggpubr_0.5.0 ggsignif_0.6.4 codetools_0.2-18 reprex_2.0.2 googleVis_0.7.0
[78] glue_1.6.2 evaluate_0.23 data.table_1.14.8 vctrs_0.6.4 tzdb_0.3.0 gtable_0.3.4 purrr_1.0.1 tidyr_1.3.0 km.ci_0.5-6 assertthat_0.2.1 ggplot2_3.5.1
[89] xfun_0.43 xtable_1.8-4 broom_1.0.3 rstatix_0.7.2 later_1.3.0 admiraldev_1.0.0 survival_3.4-0 viridisLite_0.4.2 tibble_3.2.1 websocket_1.4.1 KMsurv_0.1-5
[100] timechange_0.2.0 ellipsis_0.3.2
packageVersion("openxlsx")
[1] ‘4.2.5.1’