My loop is completely doing what I want it to do apart from when writing out the data I want it to loop through and open a new sheet each time, it is not doing this but simply overwriting the data in the single excel file. My code is:
file2 <- paste("filelocation", sep = "")
write.xlsx(Combined, file2, sheetName = (i))
I do not know why this isn't working as this exact same code is working for me earlier in the code.
Here is a simple example that writes a workbook with three identical sheets. Change the sheet contents to suit...
library(openxlsx)
wb = createWorkbook()
for (i in 1:3) {
sheet_name = paste('mtcars', i)
addWorksheet(wb, sheet_name)
writeData(wb, sheet_name, mtcars)
}
saveWorkbook(wb, 'my_workbook.xlsx')