14

The final product is an Excel CSV spreadsheet which has more than 250 columns. I was wondering if there was a way to determine the column width in Excel from R?

I am using write.csv2, which produces column width in excel all equal to 8,43.

write.csv2(df, na = "", file= "Final.csv")

If possible I am looking for a trick to vary all of them at once or only specific ones. Is running VBA from R my only option?

Thank you for your help!

3
  • I'm pretty sure that at least one of the R-to-Excel packages will let you build xlsx spreadsheets that have column-level formatting options. Commented Dec 5, 2014 at 18:16
  • 4
    There's a conceptual problem with your question. Saving a file as *.csv by definition discards all the formatting information. So when you open such a file in Excel, you get Excel's default column widths. If you want to format, you must save as *.xls or *.xlsx. Commented Dec 5, 2014 at 20:32
  • Alternatively to the package xlsx mentioned in the accepted answer, you can use the openxlsx package. I prefer this one because it does not depend on rJava. Commented Oct 13, 2017 at 11:57

4 Answers 4

21

Please check the package xlsx. I am using it for generating excel files and its pretty good. There is a method setColumnWidth which can help you. Check here for more detailed example about xlsx package functionality.


So here is a working example using package xlsx.

df <- data.frame(matrix(rnorm(100),nc=10))
library(xlsx)
# must save as an xls or xlsx file...
write.xlsx(df,"Final.xlsx", row.names=FALSE)
# load it back
wb <- loadWorkbook("Final.xlsx")
sheets <- getSheets(wb)
# set widths to 20
setColumnWidth(sheets[[1]], colIndex=1:ncol(df), colWidth=20)
saveWorkbook(wb,"Final.xlsx")
# autosize column widths
autoSizeColumn(sheets[[1]], colIndex=1:ncol(df))
saveWorkbook(wb,"Final.xlsx")
Sign up to request clarification or add additional context in comments.

1 Comment

Above code autoSizes single sheet depending on data frame we just added in that sheet, but how we can autosize all sheets from imported excel using above functionalities?
6

A small improvement to the accepted answer: Writing a file just to read and modify it again is not very elegant. Moreover, I had the experience that overwriting xls-files with saveWorkbook may lead to "corrupted" files (i.e. Excel will need to repair the file on opening it).

To avoid this, one can proceed as follows:

df <- data.frame(matrix(rnorm(100), nc=10))
library(xlsx)
wb <- createWorkbook(type = "xlsx")
sheet <- createSheet(wb, sheetName = "rnormdata")
addDataFrame(df, sheet, row.names = FALSE)
setColumnWidth(sheet, colIndex = 1:3, colWidth = 20)
autoSizeColumn(sheet, colIndex = 4:ncol(df))
saveWorkbook(wb, "Final.xlsx")

Comments

2

You can define the column width when you write a file with openxlsx:

library(openxlsx)
openxlsx::write.xlsx(df, "C:/myfile.xlsx", sheetName = "Nice Sheet", rowNames = F, colWidths=30)

1 Comment

Similarly ,you could use colWidths = "auto"
0

You can set a specific width for each column of your data set:

for(c in 1:ncol(yourData)){
setColumnWidth(sheetIndex,colIndex=c,colWidth=max(nchar(c(names(yourData[c],yourData[,c])))))
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.