Search code examples
rxlsx

write.xlsx outputting merged cells directly from R


I need to output a data frame into an excel file. The problem is that I would like to merge-center the data only for the first column. This is an easy excel function but would like to know if this is possible directly from R using xlsx or anyother library

Any help will be appreciated.

Edit: I am new to posting questions on StackOverflow so I guess I do not have rights to upload pictures. I have added the two on my google+. The first picture is what the situation is now

https://lh5.googleusercontent.com/-ednAfy8SCb8/UdcM8RB6xgI/AAAAAAAAMPA/9FuO15_UP4M/w256-h194-no/1.jpg

The second image is how I would like it to be

https://lh4.googleusercontent.com/-rU8elOT4FN8/UdcM8UZDTLI/AAAAAAAAMPE/ImNJoe5uzwk/w256-h194-no/2.jpg


Solution

  • One possibility is to use library XLConnect and function mergeCells() - only for this function you have to provide reference in for of A2:B3 and so on.

    library(XLConnect)
    #Create file
    wb <- loadWorkbook("file.xlsx", create = TRUE)
    
    # Create a worksheet called 'cars'
    createSheet(wb, name = "cars")
    
    #write data cars to sheet
    writeWorksheet(wb, cars, sheet = "cars")
    
    # Merge the cells A2:A3 and A4:A5 on the worksheet created above
    mergeCells(wb, sheet = "cars", reference = c("A2:A3","A4:A5"))
    
    # Save workbook
    saveWorkbook(wb)