Home » excel » How to append different R outputs into one excel spreadsheet

How to append different R outputs into one excel spreadsheet

Posted by: admin May 14, 2020 Leave a comment

Questions:

So whenever I want to run my code seperately for different datasets…I want the output from my code to be saved in the same excel spreadsheet but at different sheets….So If I run my code for 20 different datasets…I would want all the output to saved in the same excel spreadsheet but different worksheets…so I would have 20 worksheets in a single excel spreadsheet…is there a special function in r that would let me do this?…..so lets say my existing spreadsheet is called ‘Values.csv’….How would I append the rest of my output to this same spreadsheet.

I usually just use write.csv(data,'Values.csv') etc….But I’m not sure how to append my output to this same worksheet…

How to&Answers:

You can use library XLConnect to do this.

library(XLConnect)

#some sample data
your.data=data.frame(a=1:10,b=21:30)

#Create .xls file
wb <- loadWorkbook("newfile.xls", create = TRUE)

#Create Sheet in file
createSheet(wb,name="name_one")

#write data
writeWorksheet(wb,your.data,sheet="name_one")

#save .xls file
saveWorkbook(wb)