R:如何使用 R 代码冻结导出的 Excel 文件中的第一行?

R: How can you freeze the top row in an exported Excel file using R code?

解决方案必须在 R 中。 相关问题正在为其他语言提供解决方案。 (例如,此处 freeze top row using spreadsheetgear

的 Excel 对应物
library(tidyverse)
mtcars %>% write_csv('IneedExcelNotCSV.csv')

部分代码冻结了第一行。

类似于 .xls(x)(例如 OpenOffice)格式的解决方案也可以。 (.sxc?)

如果您使用 XLSX R 包保存为 .xlsx 文件,您可以添加

 createFreezePane(sheet, 2, 1, startRow = 2, startColumn = 1)

并且第一行被冻结。

您可以在使用 openxlsx 包通过 freezePane 参数将数据导出为 .xlsx 文件时冻结窗格:firstActiveRowfirstActiveColfirstRowfirstCol.

例如,要冻结第一行:

library(openxlsx)
write.xlsx(sheetname, file = filename, firstRow = TRUE) #freezes top row
write.xlsx(sheetname, file = filename, firstRow = TRUE, firstCol = TRUE) #freezes top row and first column
write.xlsx(sheetname, file = filename, firstActiveRow = 4, firstActiveCol = 3) #freezes fourth row and third column