Writexl will write to separate tabs in the workbook if the data.frames are split into a named list
library("tidyverse")
library("lubridate")
#>
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#>
#> date, intersect, setdiff, union
library("writexl")
# Using a toy data set
set.seed(123)
dat <- tibble(
id = rpois(7, 15),
name = letters[1:7],
test = sample(1:2, size = 7, replace = TRUE),
date = dmy(c("02/04/2012", "15/07/2008", "23/11/2016", "14/12/2017",
"16/05/2015", "26/08/2018", "15/01/2012")),
class = sample(LETTERS[1:4], size = 7, replace = TRUE)
)
dat_split <- dat %>%
split(dat, f = dat$class)
dat_split
#> $A
#> # A tibble: 2 × 5
#> id name test date class
#> <int> <chr> <int> <date> <chr>
#> 1 12 a 1 2012-04-02 A
#> 2 10 g 1 2012-01-15 A
#>
#> $B
#> # A tibble: 2 × 5
#> id name test date class
#> <int> <chr> <int> <date> <chr>
#> 1 15 d 1 2017-12-14 B
#> 2 16 f 1 2018-08-26 B
#>
#> $C
#> # A tibble: 2 × 5
#> id name test date class
#> <int> <chr> <int> <date> <chr>
#> 1 19 b 1 2008-07-15 C
#> 2 21 e 2 2015-05-16 C
#>
#> $D
#> # A tibble: 1 × 5
#> id name test date class
#> <int> <chr> <int> <date> <chr>
#> 1 8 c 1 2016-11-23 D
write_xlsx(dat_split, path = "~/Desktop/dat.xlsx",
format_headers = FALSE)
Created on 2022-04-23 by the reprex package (v2.0.1)