benh56
benh56

Reputation: 37

Export elements of a list to separate excel files in R

I've got a list of 24 dataframes and I'm wondering if there's an easy way to export each element to its own excel file from R, preferably using the name of the list element in the excel files title?

I know I can do write.xlsx(listofdfs, "newexcelfilename.xlsx") and it will put each element into its own worksheet within the file, but in my case I'll eventually need to email these files separately so it is best if I can get them into their own individual excel file. Thanks in advance!

Upvotes: 1

Views: 982

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389275

You may use any of the apply function in base R. For example, using Map -

Map(openxlsx::write.xlsx, listofdfs, paste0(names(listofdfs), '.xlsx'))

Also with purrr::imap -

purrr::imap(listofdfs, ~openxlsx::write.xlsx(.x, paste0(.y, '.xlsx')))

Upvotes: 1

Related Questions