Search code examples
rexcelexport-to-excel

Create an Excel file with the name of sheets taken from a vector


Starting from the vector:

vector <- c("alfa", "beta", "gamma", "delta", "epsilon")

I would like to create an Excel file with the name of sheet1 = alfa, the name of sheet2 = beta, etc.

Is there a way?

Actually, I need to export multiple dataframes in an .xlsx format and I use the package "writexl" and these instructions:

  sheets <- list("sheet1Name" = dataframe1, "sheet2Name" = dataframe2)
  write_xlsx(sheets, "data.xlsx")
 

but I have to write the "sheet*Name" manually, and I have more than 100 sheets... For this reason I would like to take their names from a vector.


Solution

  • We could just use setNames or assign the names with the vector values

    sheets <- mget(ls(pattern = '^dataframe\\d+$'))
    names(sheets) <- vector