-1

I would like to create two tabs with Terms and Commloans. But when I am using this code, Terms are overwriting Commloans and I don't have these two tabs :/

library("openxlsx")
write.xlsx(Comms, file = "PnL.xlsx", sheetName="Commloans", append=TRUE)
write.xlsx(Terms, file = "PnL.xlsx", sheetName="Terms", append=TRUE)
zx8754
  • 52,746
  • 12
  • 114
  • 209
KennyR
  • 5
  • 1

2 Answers2

0

it is hard to answer without a reprex, but I guess if you remove the append = TRUE from the first call and only leave it on the second, this should work using the xlsx package

So...

xlsx::write.xlsx(Comms, file = "PnL.xlsx", sheetName="Commloans")
xlsx::write.xlsx(Terms, file = "PnL.xlsx", sheetName="Terms", append=TRUE)
FMM
  • 1,857
  • 1
  • 15
  • 38
0

You need to create some worksheets first and then use write.xlsx. See this answer; R: easy way to export multiple data.frame to multiple excel worksheets?

I would recommend using the openxlsx package

library(openxlsx)

# Create a blank workbook
OUT <- createWorkbook()

# Add some sheets to the workbook
addWorksheet(OUT, "Sheet 1 Name")
addWorksheet(OUT, "Sheet 2 Name")

# Write the data to the sheets
writeData(OUT, sheet = "Sheet 1 Name", x = dataframe1)
writeData(OUT, sheet = "Sheet 2 Name", x = dataframe2)

# Export the file
saveWorkbook(OUT, "My output file.xlsx")
EcologyTom
  • 2,344
  • 2
  • 27
  • 38