How to customize the appearance of data to download in Shiny ?

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)

This is not a question about Shiny, is it?
What do you want your data to look like in the end? Would dplyr::bind_cols solve it for you?

1 Like

Thanks for suggestion. bind_cols would have solved if no spaces between data appended.

But as there are unequal sized spacing between data being appended.

Key reason to include in Shiny beause when the user click download, he needs in such format (data1, ...data4)

Oh, you mean they will get 4 datasets out? Then you can create either one file with 4 sheets or a zip folder with 4 files in it. I'm not sure if it's possible to send 4 different files at once (I would guess, it's not possible).

Thanks for your insight. we have updated the question.

If it was not within shiny, we would have used worksheet.
Can we do something like that within Shiny ?