Using mergeCells() in openxlsx package R creates error in output





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I'm trying to output a data frame in R into excel but I keep getting an error when I use mergeCells() when trying to open the resulting xlsx file. While the cells do merge, my data is "lost." I can unmerge the cells and the data is there but I want to format it so the output (ex. column 1 of my df) spans over multiple columns.



I've tried merging the cells before and after writing the data to the worksheet. I've also tried using writeDataTable() and writeData(), both did not work. I've tried starting the df on different columns (as seen below). For example, start writing the df to column 2, and merge columns 1:2. The other one I merged columns 1:2 first then wrote data starting on column 1.



df <- data.frame(
Category = c("A", "B", "C"),
Type = c("x", "y", "z"),
Number = c("1", "2", "3"), stringsAsFactors = FALSE)

book <- createWorkbook()
sheet <- "Sheet1"
writeData(book, sheet, df, startCol = 2, startRow = 1, colNames = TRUE)
mergeCells(book, sheet, cols = 1:2, rows = 1)
mergeCells(book, sheet, cols = 1:2, rows = 2)
mergeCells(book, sheet, cols = 1:2, rows = 3)
saveWorkbook(book)


OR



mergeCells(book, sheet, cols = 1:2, rows = 1)
mergeCells(book, sheet, cols = 1:2, rows = 2)
mergeCells(book, sheet, cols = 1:2, rows = 3)
writeDataTable(book, sheet, df, startCol = 1, startRow = 1, colNames = TRUE)
saveWorkbook(book)


When opening the file after saving, the error is "We found a problem with some content, etc. Excel was able to open the file by removing or repairing unreadable content."



Any help is appreciated!










share|improve this question























  • It's not clear what you want as the final answer. You are writing 3 columns. You want the format of column 1 to be applied to column 1 and column 2. You want to see the content of column 1, column 2 and column 3. Is that correct?

    – awchisholm
    Jan 4 at 17:44











  • @awchisholm I want column 1 to have merged cells while column 2 and 3 are in unmerged cells. So the excel output has column 1 merged into cells A:B and column 2 in cell C and column 3 in cell D. I hope that explained it better.

    – SGarg
    Jan 4 at 17:52


















1















I'm trying to output a data frame in R into excel but I keep getting an error when I use mergeCells() when trying to open the resulting xlsx file. While the cells do merge, my data is "lost." I can unmerge the cells and the data is there but I want to format it so the output (ex. column 1 of my df) spans over multiple columns.



I've tried merging the cells before and after writing the data to the worksheet. I've also tried using writeDataTable() and writeData(), both did not work. I've tried starting the df on different columns (as seen below). For example, start writing the df to column 2, and merge columns 1:2. The other one I merged columns 1:2 first then wrote data starting on column 1.



df <- data.frame(
Category = c("A", "B", "C"),
Type = c("x", "y", "z"),
Number = c("1", "2", "3"), stringsAsFactors = FALSE)

book <- createWorkbook()
sheet <- "Sheet1"
writeData(book, sheet, df, startCol = 2, startRow = 1, colNames = TRUE)
mergeCells(book, sheet, cols = 1:2, rows = 1)
mergeCells(book, sheet, cols = 1:2, rows = 2)
mergeCells(book, sheet, cols = 1:2, rows = 3)
saveWorkbook(book)


OR



mergeCells(book, sheet, cols = 1:2, rows = 1)
mergeCells(book, sheet, cols = 1:2, rows = 2)
mergeCells(book, sheet, cols = 1:2, rows = 3)
writeDataTable(book, sheet, df, startCol = 1, startRow = 1, colNames = TRUE)
saveWorkbook(book)


When opening the file after saving, the error is "We found a problem with some content, etc. Excel was able to open the file by removing or repairing unreadable content."



Any help is appreciated!










share|improve this question























  • It's not clear what you want as the final answer. You are writing 3 columns. You want the format of column 1 to be applied to column 1 and column 2. You want to see the content of column 1, column 2 and column 3. Is that correct?

    – awchisholm
    Jan 4 at 17:44











  • @awchisholm I want column 1 to have merged cells while column 2 and 3 are in unmerged cells. So the excel output has column 1 merged into cells A:B and column 2 in cell C and column 3 in cell D. I hope that explained it better.

    – SGarg
    Jan 4 at 17:52














1












1








1








I'm trying to output a data frame in R into excel but I keep getting an error when I use mergeCells() when trying to open the resulting xlsx file. While the cells do merge, my data is "lost." I can unmerge the cells and the data is there but I want to format it so the output (ex. column 1 of my df) spans over multiple columns.



I've tried merging the cells before and after writing the data to the worksheet. I've also tried using writeDataTable() and writeData(), both did not work. I've tried starting the df on different columns (as seen below). For example, start writing the df to column 2, and merge columns 1:2. The other one I merged columns 1:2 first then wrote data starting on column 1.



df <- data.frame(
Category = c("A", "B", "C"),
Type = c("x", "y", "z"),
Number = c("1", "2", "3"), stringsAsFactors = FALSE)

book <- createWorkbook()
sheet <- "Sheet1"
writeData(book, sheet, df, startCol = 2, startRow = 1, colNames = TRUE)
mergeCells(book, sheet, cols = 1:2, rows = 1)
mergeCells(book, sheet, cols = 1:2, rows = 2)
mergeCells(book, sheet, cols = 1:2, rows = 3)
saveWorkbook(book)


OR



mergeCells(book, sheet, cols = 1:2, rows = 1)
mergeCells(book, sheet, cols = 1:2, rows = 2)
mergeCells(book, sheet, cols = 1:2, rows = 3)
writeDataTable(book, sheet, df, startCol = 1, startRow = 1, colNames = TRUE)
saveWorkbook(book)


When opening the file after saving, the error is "We found a problem with some content, etc. Excel was able to open the file by removing or repairing unreadable content."



Any help is appreciated!










share|improve this question














I'm trying to output a data frame in R into excel but I keep getting an error when I use mergeCells() when trying to open the resulting xlsx file. While the cells do merge, my data is "lost." I can unmerge the cells and the data is there but I want to format it so the output (ex. column 1 of my df) spans over multiple columns.



I've tried merging the cells before and after writing the data to the worksheet. I've also tried using writeDataTable() and writeData(), both did not work. I've tried starting the df on different columns (as seen below). For example, start writing the df to column 2, and merge columns 1:2. The other one I merged columns 1:2 first then wrote data starting on column 1.



df <- data.frame(
Category = c("A", "B", "C"),
Type = c("x", "y", "z"),
Number = c("1", "2", "3"), stringsAsFactors = FALSE)

book <- createWorkbook()
sheet <- "Sheet1"
writeData(book, sheet, df, startCol = 2, startRow = 1, colNames = TRUE)
mergeCells(book, sheet, cols = 1:2, rows = 1)
mergeCells(book, sheet, cols = 1:2, rows = 2)
mergeCells(book, sheet, cols = 1:2, rows = 3)
saveWorkbook(book)


OR



mergeCells(book, sheet, cols = 1:2, rows = 1)
mergeCells(book, sheet, cols = 1:2, rows = 2)
mergeCells(book, sheet, cols = 1:2, rows = 3)
writeDataTable(book, sheet, df, startCol = 1, startRow = 1, colNames = TRUE)
saveWorkbook(book)


When opening the file after saving, the error is "We found a problem with some content, etc. Excel was able to open the file by removing or repairing unreadable content."



Any help is appreciated!







r dataframe openxlsx






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 4 at 17:00









SGargSGarg

234




234













  • It's not clear what you want as the final answer. You are writing 3 columns. You want the format of column 1 to be applied to column 1 and column 2. You want to see the content of column 1, column 2 and column 3. Is that correct?

    – awchisholm
    Jan 4 at 17:44











  • @awchisholm I want column 1 to have merged cells while column 2 and 3 are in unmerged cells. So the excel output has column 1 merged into cells A:B and column 2 in cell C and column 3 in cell D. I hope that explained it better.

    – SGarg
    Jan 4 at 17:52



















  • It's not clear what you want as the final answer. You are writing 3 columns. You want the format of column 1 to be applied to column 1 and column 2. You want to see the content of column 1, column 2 and column 3. Is that correct?

    – awchisholm
    Jan 4 at 17:44











  • @awchisholm I want column 1 to have merged cells while column 2 and 3 are in unmerged cells. So the excel output has column 1 merged into cells A:B and column 2 in cell C and column 3 in cell D. I hope that explained it better.

    – SGarg
    Jan 4 at 17:52

















It's not clear what you want as the final answer. You are writing 3 columns. You want the format of column 1 to be applied to column 1 and column 2. You want to see the content of column 1, column 2 and column 3. Is that correct?

– awchisholm
Jan 4 at 17:44





It's not clear what you want as the final answer. You are writing 3 columns. You want the format of column 1 to be applied to column 1 and column 2. You want to see the content of column 1, column 2 and column 3. Is that correct?

– awchisholm
Jan 4 at 17:44













@awchisholm I want column 1 to have merged cells while column 2 and 3 are in unmerged cells. So the excel output has column 1 merged into cells A:B and column 2 in cell C and column 3 in cell D. I hope that explained it better.

– SGarg
Jan 4 at 17:52





@awchisholm I want column 1 to have merged cells while column 2 and 3 are in unmerged cells. So the excel output has column 1 merged into cells A:B and column 2 in cell C and column 3 in cell D. I hope that explained it better.

– SGarg
Jan 4 at 17:52












1 Answer
1






active

oldest

votes


















3














If I understand your question correctly, you want to merge columns 1 and 2 to produce a merged column with the first column of df. If this is correct, then your issue is that the left most column to which you are merging (column 1) is blank. openxlsx::mergeCells() will then keep the content of the left-most column merged. To merge columns 1 and 2 and have the contents of df[1] in it, you need to write the content of df[1] on the left-most column like so:



library(openxlsx)

df <- data.frame(
Category = c("A", "B", "C"),
Type = c("x", "y", "z"),
Number = c("1", "2", "3"), stringsAsFactors = FALSE)

wb <- createWorkbook() # creates workbook
addWorksheet(wb, "Sheet1") # adds sheet
writeData(wb, 1, df[1], startCol = 1, startRow = 1, colNames = TRUE) # writing content on the left-most column to be merged
writeData(wb, 1, df[2:3], startCol = 3, startRow = 1, colNames = TRUE) # write the rest of the content on the columns that wont be merged
for(i in seq_len(nrow(df) + 1)){ # loop over rows for merging
mergeCells(wb, 1, cols = 1:2, rows = i)
}
rm(i)
saveWorkbook(wb, "test.xlsx", overwrite = T) # save workbook





share|improve this answer
























  • thank you @JdeMello that worked great

    – SGarg
    Jan 4 at 18:51













  • @Sgarg you're welcome! You're really close to get it right Cheers

    – JdeMello
    Jan 4 at 18:56






  • 1





    Thank you for this answer. I had a similar problem to merge a region with multiple rows. For future readers of this thread, I would add that, more generally, mergeCells keeps the content of the top-left cell.

    – Stéphane Laurent
    Feb 13 at 10:33












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54043185%2fusing-mergecells-in-openxlsx-package-r-creates-error-in-output%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














If I understand your question correctly, you want to merge columns 1 and 2 to produce a merged column with the first column of df. If this is correct, then your issue is that the left most column to which you are merging (column 1) is blank. openxlsx::mergeCells() will then keep the content of the left-most column merged. To merge columns 1 and 2 and have the contents of df[1] in it, you need to write the content of df[1] on the left-most column like so:



library(openxlsx)

df <- data.frame(
Category = c("A", "B", "C"),
Type = c("x", "y", "z"),
Number = c("1", "2", "3"), stringsAsFactors = FALSE)

wb <- createWorkbook() # creates workbook
addWorksheet(wb, "Sheet1") # adds sheet
writeData(wb, 1, df[1], startCol = 1, startRow = 1, colNames = TRUE) # writing content on the left-most column to be merged
writeData(wb, 1, df[2:3], startCol = 3, startRow = 1, colNames = TRUE) # write the rest of the content on the columns that wont be merged
for(i in seq_len(nrow(df) + 1)){ # loop over rows for merging
mergeCells(wb, 1, cols = 1:2, rows = i)
}
rm(i)
saveWorkbook(wb, "test.xlsx", overwrite = T) # save workbook





share|improve this answer
























  • thank you @JdeMello that worked great

    – SGarg
    Jan 4 at 18:51













  • @Sgarg you're welcome! You're really close to get it right Cheers

    – JdeMello
    Jan 4 at 18:56






  • 1





    Thank you for this answer. I had a similar problem to merge a region with multiple rows. For future readers of this thread, I would add that, more generally, mergeCells keeps the content of the top-left cell.

    – Stéphane Laurent
    Feb 13 at 10:33
















3














If I understand your question correctly, you want to merge columns 1 and 2 to produce a merged column with the first column of df. If this is correct, then your issue is that the left most column to which you are merging (column 1) is blank. openxlsx::mergeCells() will then keep the content of the left-most column merged. To merge columns 1 and 2 and have the contents of df[1] in it, you need to write the content of df[1] on the left-most column like so:



library(openxlsx)

df <- data.frame(
Category = c("A", "B", "C"),
Type = c("x", "y", "z"),
Number = c("1", "2", "3"), stringsAsFactors = FALSE)

wb <- createWorkbook() # creates workbook
addWorksheet(wb, "Sheet1") # adds sheet
writeData(wb, 1, df[1], startCol = 1, startRow = 1, colNames = TRUE) # writing content on the left-most column to be merged
writeData(wb, 1, df[2:3], startCol = 3, startRow = 1, colNames = TRUE) # write the rest of the content on the columns that wont be merged
for(i in seq_len(nrow(df) + 1)){ # loop over rows for merging
mergeCells(wb, 1, cols = 1:2, rows = i)
}
rm(i)
saveWorkbook(wb, "test.xlsx", overwrite = T) # save workbook





share|improve this answer
























  • thank you @JdeMello that worked great

    – SGarg
    Jan 4 at 18:51













  • @Sgarg you're welcome! You're really close to get it right Cheers

    – JdeMello
    Jan 4 at 18:56






  • 1





    Thank you for this answer. I had a similar problem to merge a region with multiple rows. For future readers of this thread, I would add that, more generally, mergeCells keeps the content of the top-left cell.

    – Stéphane Laurent
    Feb 13 at 10:33














3












3








3







If I understand your question correctly, you want to merge columns 1 and 2 to produce a merged column with the first column of df. If this is correct, then your issue is that the left most column to which you are merging (column 1) is blank. openxlsx::mergeCells() will then keep the content of the left-most column merged. To merge columns 1 and 2 and have the contents of df[1] in it, you need to write the content of df[1] on the left-most column like so:



library(openxlsx)

df <- data.frame(
Category = c("A", "B", "C"),
Type = c("x", "y", "z"),
Number = c("1", "2", "3"), stringsAsFactors = FALSE)

wb <- createWorkbook() # creates workbook
addWorksheet(wb, "Sheet1") # adds sheet
writeData(wb, 1, df[1], startCol = 1, startRow = 1, colNames = TRUE) # writing content on the left-most column to be merged
writeData(wb, 1, df[2:3], startCol = 3, startRow = 1, colNames = TRUE) # write the rest of the content on the columns that wont be merged
for(i in seq_len(nrow(df) + 1)){ # loop over rows for merging
mergeCells(wb, 1, cols = 1:2, rows = i)
}
rm(i)
saveWorkbook(wb, "test.xlsx", overwrite = T) # save workbook





share|improve this answer













If I understand your question correctly, you want to merge columns 1 and 2 to produce a merged column with the first column of df. If this is correct, then your issue is that the left most column to which you are merging (column 1) is blank. openxlsx::mergeCells() will then keep the content of the left-most column merged. To merge columns 1 and 2 and have the contents of df[1] in it, you need to write the content of df[1] on the left-most column like so:



library(openxlsx)

df <- data.frame(
Category = c("A", "B", "C"),
Type = c("x", "y", "z"),
Number = c("1", "2", "3"), stringsAsFactors = FALSE)

wb <- createWorkbook() # creates workbook
addWorksheet(wb, "Sheet1") # adds sheet
writeData(wb, 1, df[1], startCol = 1, startRow = 1, colNames = TRUE) # writing content on the left-most column to be merged
writeData(wb, 1, df[2:3], startCol = 3, startRow = 1, colNames = TRUE) # write the rest of the content on the columns that wont be merged
for(i in seq_len(nrow(df) + 1)){ # loop over rows for merging
mergeCells(wb, 1, cols = 1:2, rows = i)
}
rm(i)
saveWorkbook(wb, "test.xlsx", overwrite = T) # save workbook






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 4 at 17:54









JdeMelloJdeMello

816419




816419













  • thank you @JdeMello that worked great

    – SGarg
    Jan 4 at 18:51













  • @Sgarg you're welcome! You're really close to get it right Cheers

    – JdeMello
    Jan 4 at 18:56






  • 1





    Thank you for this answer. I had a similar problem to merge a region with multiple rows. For future readers of this thread, I would add that, more generally, mergeCells keeps the content of the top-left cell.

    – Stéphane Laurent
    Feb 13 at 10:33



















  • thank you @JdeMello that worked great

    – SGarg
    Jan 4 at 18:51













  • @Sgarg you're welcome! You're really close to get it right Cheers

    – JdeMello
    Jan 4 at 18:56






  • 1





    Thank you for this answer. I had a similar problem to merge a region with multiple rows. For future readers of this thread, I would add that, more generally, mergeCells keeps the content of the top-left cell.

    – Stéphane Laurent
    Feb 13 at 10:33

















thank you @JdeMello that worked great

– SGarg
Jan 4 at 18:51







thank you @JdeMello that worked great

– SGarg
Jan 4 at 18:51















@Sgarg you're welcome! You're really close to get it right Cheers

– JdeMello
Jan 4 at 18:56





@Sgarg you're welcome! You're really close to get it right Cheers

– JdeMello
Jan 4 at 18:56




1




1





Thank you for this answer. I had a similar problem to merge a region with multiple rows. For future readers of this thread, I would add that, more generally, mergeCells keeps the content of the top-left cell.

– Stéphane Laurent
Feb 13 at 10:33





Thank you for this answer. I had a similar problem to merge a region with multiple rows. For future readers of this thread, I would add that, more generally, mergeCells keeps the content of the top-left cell.

– Stéphane Laurent
Feb 13 at 10:33




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54043185%2fusing-mergecells-in-openxlsx-package-r-creates-error-in-output%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas