Home > Back-end >  R: Export Multiple Excel Workbooks from dataframe
R: Export Multiple Excel Workbooks from dataframe

Time:08-11

I have a dataframe of deals by person. I have multiple people (30 ) and I want to send them a report of just their deals. Rather than manually creating 30 individual excel workbooks, is there a package in R that can do this?

Below is my dummy data

df <- tibble(
  Deal = c("Deal A", "Deal B", "Deal C", "Deal D", "Deal E", "Deal F", "Deal G", "Deal H"),
  Person = c("john", "henry", "max", "felix", "henry", "john", "max", "max")
)

The expected result of the dummy data is to have 4 (by person) separate excel workbooks with deal data so I can send it to the people via email.

Bonus points if I can format workbooks to all have bold header and format table with $ for numbers and dates for dates!

CodePudding user response:

To expand on my comment above:

library(tidyverse)
# Other packages for handling xlsx files are available
library(xlsx)
df %>% 
  group_by(Person) %>% 
  group_walk(
    function(.x, .y) {
      write.xlsx(.x, paste0(.y$Person, ".xlsx"), sheet="Sheet1")
    }
  )

After running this code, I see the following files in my current working directory

enter image description here

  • Related