Попробуйте:
Кажется, много работы для чего-то такого простого в excel
, но я нашел это с помощью openxlsx
, вы можете делать большинство вещей с небольшим усилием, мне очень нравится пакет.
txt <- data.frame(t = c("I want this in two lines", "in one cell"), stringsAsFactors = FALSE)
wb <- createWorkbook()
addWorksheet(wb, "Sheet 1")
writeData(wb, "Sheet 1", x = txt)
# to make sure your text shows as two lines
setRowHeights(wb, "Sheet 1", rows = 4, height = 30)
# this seems to force the line break to split the text over two lines
style1 <- createStyle(wrapText = TRUE)
addStyle(wb, sheet = 1, style1, rows = 4, cols = 1, gridExpand = TRUE)
#narrow width produces multiple lines as a consequence of text wrap
setColWidths(wb, sheet = 1, cols = 1, widths = 40)
#Here's the key bit; excel does not seem to like \n as a line break an online search suggested CHAR(10) as the alternative to `alt enter` keystroke for line break in an excel cell
writeFormula(wb, 1, x = "A2&CHAR(10)&A3", startCol = 1, startRow = 4)
saveWorkbook(wb, file = "foo.xlsx", overwrite = TRUE)
Что приводит к: