How to resolve autoSizeColumn issues for downloadable xlsx file

Hi,

I have this very basic shiny app below. I am hoping there is a way I can auto size all the columns based on the properties of the sheet and not the actual data.frame as my actual coding scenario is more involved.

Currently, it doesn't work as you will see. If you change i to 1:1 it will work but that doesn't make a lot of sense as we technically have 4 columns in the output.

I would ideally like to solve this with base or xlsx directly and not introduce another dependency.

Other relevant information:
R version 4.0.2
xlsx_0.6.5
rJava_1.0-6

library(shiny)
library(xlsx)

ui <- fluidPage(
  downloadButton("download", "Download XLSX file")
)

server <- function(input, output) {
  output$download <- downloadHandler(
    filename = function() {
      "my_file.xlsx"
    },
    content = function(file) {
      # Create sample data
      my_data <- data.frame(a = 1:10, b = letters[1:10], c = 11:20)

      # Create a new workbook
      wb <- createWorkbook()

      # Add a new sheet
      sheet <- createSheet(wb, sheetName = "My Data")

      xlsx::addDataFrame(my_data,sheet)


      for (i in 1:3) {
            autoSizeColumn(sheet, i)
          }

      # Save the workbook
      saveWorkbook(wb, file)
    }
  )
}

shinyApp(ui, server)

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.