Basically, we are trying to concatenate the data
in the following way described in server function.
User will have the data in a formatted way after he/she downloads.
usual cbind doesnt work because
different column names and spacing is different here
library(writexl)
ui <- fluidPage(
downloadButton("dl", "Download")
)
server <- function(input, output) {
data1 = mtcars[,c(1,2)] %>% head() # data for Col 1 ,until Row 6
data2 = gapminder::gapminder[,c(1,4)] %>% head() # data for Col 1 , Row from 8 until Row 13
data3 = mtcars[,c(1,2)] %>% tail() # data for Col 1 , Row from 15 until Row 20
data4 = gapminder::gapminder[,c(1,4)] %>% tail() # data for Col 25 , Row from 8 until Row 30
data = # combination of data1, data2,data3 and data4
output$dl <- downloadHandler(
filename = function() { ".xlsx"},
content = function(file) {write_xlsx(data, path = file)}
)
}
shinyApp(ui, server)
If it was not in Shiny, we would have taken the following approach:
wb <- createWorkbook()
addWorksheet(wb, sheetName = # sheet name
)
writeData(wb, sheet = 1, x = data1, startCol = 1, startRow = 1)
writeData(wb, sheet = 1, x = data2, startCol = 1, startRow = 8)
writeData(wb, sheet = 1, x = data3, startCol = 1, startRow = 15)
writeData(wb, sheet = 1, x = data4, startCol = 1, startRow = 25)
saveWorkbook(wb, file = "my_appended_file", overwrite = TRUE)