Sounds like a bad idea to intersperse data frame names with data in the same worksheet, but that's up to you.
Anyway, use openxlsx
. Here's a way to do it:
dfname1 <- data.frame(col1 = c("x", "x"), col2 = c("x", "x"), stringsAsFactors = FALSE)
dfname2 <- data.frame(col1 = c("x"), col3 = c("x"), stringsAsFactors = FALSE)
df_list <- list(dfname1=dfname1,
dfname2=dfname2)
library(openxlsx)
wb <- createWorkbook()
addWorksheet(wb, "Foo")
curr_row <- 1
for(i in seq_along(df_list)) {
writeData(wb, "Foo", names(df_list)[i], startCol = 1, startRow = curr_row)
writeData(wb, "Foo", df_list[[i]], startCol = 1, startRow = curr_row+1)
curr_row <- curr_row + nrow(df_list[[i]]) + 2
}
saveWorkbook(wb, "bar.xlsx")
This gives you (literally) what you asked for.