我试图使用openxlsx (或xlsx或其他包)包将数据帧导出到excel电子表格。我遇到的一个问题是,我希望将某些列设置为"text“而不是"general”,因为Excel倾向于自动格式化基因名(即SEPT16 -> 16-Sep (日期格式))。
openxlsx文档提供了一些示例,用于将列类设置为“货币”、“会计”、“超链接”、“百分比”或“科学”,但没有明确设置为“文本”。我尝试将类设置为"text“或”字符“,但output Excel列仍然是”通用“的。最初,正确的文本存在,但是如果我在单元格中编辑任何内容,Excel会自动格式化这些单元格。
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")发布于 2016-10-27 16:07:32
openxlsx确实允许文本格式设置,但它需要几个步骤:
wb并为其创建一个选项卡(工作表)。wb中的工作表分配(写入)一些数据样本代码
# Create workbook & sheet:
wb <- openxlsx::createWorkbook()
sheet <- openxlsx::addWorksheet(wb, "test")
# Create the cell style
textstyle <- openxlsx::createStyle(fontName = "Arial", fontSize = 7, numFmt = "@")
# Assign df to workbook
openxlsx::writeDataTable(wb = wb, sheet = "test", x = df)
# First identify the range of the 'text cells':
textcells <- expand.grid(row = c(1,3,5), col = c(1,2,3,4,5))
# Then assign 'textstyle' to the 'textcells-range':
openxlsx::addStyle(wb = wb, sheet = "test",
rows = textcells$row, cols = textcells$col,
style = textstyle)
# Save the workbook
openxlsx::saveWorkbook(wb, "example_table.xlsx")https://stackoverflow.com/questions/40287407
复制相似问题