I am trying to use openxlsx (or xlsx or other package) package to export data frames to excel spreadsheets. One problem that I have is that I want to set certain columns to "text" rather than "general" because Excel has a tendency to automatically format gene names (i.e SEPT16 -> 16-Sep (date format)).
The openxlsx documentation has some examples for setting column classes to "currency", "accounting", "hyperlink", "percentage", or "scientific", but not explicitly to "text". I've tried setting the class to "text" or "character", but the output Excel column is still "general". Initially, the correct text is there, but if I edit anything in the cell, Excel automatically formats these cells.
library(openxlsx)
df <- data.frame(gene = c("SEPT16", "MARCH10", "GATA4"),
pvalue = c(0.0123, 0.2315, 0.00001),
stringsAsFactors = FALSE)
class(df$gene) <- "text" # Doesn't work
class(df$pvalue) <- "scientific"
wb <- openxlsx::createWorkbook()
sheet <- openxlsx::addWorksheet(wb, "test")
openxlsx::writeDataTable(wb = wb,
sheet = "test",
x = df)
openxlsx::saveWorkbook(wb, "example_table.xlsx")