1

I read a file, change its content, and then I want to write the dataframe into a new file. The thing that bugs me is that the width of the columns isn't adjustable within Excel (it does not save changes). I was wondering if it is possible to write the csv file with column width that fits the longest value.

dat <- read.csv("Input.csv")

# Do some processing

#Write the new file
write.csv(dat, "Output.csv", row.names=FALSE)

Edit 1:

dat <- read.csv("Input.csv")
createSheet(wb, "test")
writeWorksheet(wb, dat, "test")
colWidths <- sapply(1:ncol(dat), function(i) max(c(8, nchar(c(names(dat)[i], as.character(dat[, i]))))))
setColumnWidth(wb, "test", 1:ncol(dat), colWidths * 256)
saveWorkbook(wb)

what did I do wrong? It writes an empty file.

4
  • CSV is a plain text format, I can't see how there would be a way to directly do this. I suppose you could store a vector of desired column widths as the last row of the csv, then write some kind of excel macro to apply those fields to an imported CSV. But that seems like overkill... Or, dunno, maybe try appending a whitespace row at the bottom of the desired length/column? No idea if that would work. Commented Jul 8, 2015 at 19:03
  • 1
    See write.fwf in library(gdata) inside-r.org/packages/cran/gdata/docs/write.fwf Commented Jul 8, 2015 at 19:03
  • minimal code is highly encouraged. this question seems more about adjusting column widths in excel spreadsheets anyway Commented Jul 8, 2015 at 19:12
  • @rawr completely agree. Have trimmed the code down to the bare minimum Commented Jul 8, 2015 at 19:42

2 Answers 2

1

It doesn't matter what widths you write for your csv; Excel will always have its default column width when you open it.

Your options are:

  1. Accept this behaviour.
  2. Resave the file from Excel as something else (.xls or .xlsx)
  3. Write the file from R using a package that directly exports Excel files. XLConnect will do this and even has a setColumnWidth function to set the column widths within R.

e.g.

dat <- data.frame(x = 1:24, `Long Column Name` = 25:48, `Wide Column` = paste(LETTERS, collapse = " "))
library("XLConnect")
wb <- loadWorkbook("Output.xlsx", create = TRUE)
createSheet(wb, "Output")
writeWorksheet(wb, dat, "Output")
colWidths <- sapply(1:ncol(dat), function(i) max(c(8, nchar(c(names(dat)[i], as.character(dat[, i])))))
setColumnWidth(wb, "Output", 1:ncol(dat), colWidths * 256)
saveWorkbook(wb)
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, but for some reason it gives me an empty file. I added the code that I tried to my original question.
@Nik you're missing the loadWorkbook line, but I wouldn't have thought that was the issue. Is the .xlsx file a valid spreadsheet? Does it have any worksheets at all? What are you using to open it?
I am so stupid that it's funny (to me). The missing line fixed it. Thank you! It works great now! Do you know if I can choose the directory of the file I am saving though?
@Nik just specify a full name to the loadWorkbook function, e.g. (on Windows) "c:/folder/subfolder/file.xlsx")
0

You may need to close the .csv in Excel before you run write.csv in R because Excel locks the file.

It is also possible to pad the columns like this if that is what you want.

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.