'openxlsx::writeData() cut the column names of my dataset even if I force colNames=TRUE

I have several cities with stats stored into dataframes lists (each list have one dataframe for each city). I want to create an excel sheet with all the dataframes for each city. Each dataframe will be stored in a different tab.

This is my code :

pblapply(Communes$Commune, FUN=function(x) {
    message(sprintf("Commune de %s",x))
    xPath <- sprintf("output/%s/Fiche Communale %s.xlsx",x,x)
    fiche <- openxlsx::createWorkbook(sprintf("Fiche Communale %s",x))
    lapply(list_file$Tables, FUN=function(f) {
      xSheet=tools::file_path_sans_ext(f)
      openxlsx::addWorksheet(fiche,xSheet)
      openxlsx::writeData(fiche, xSheet, eval(parse(text = xSheet))[x], colNames=TRUE)
      openxlsx::saveWorkbook(fiche, xPath, overwrite = TRUE)
    })
  })

I'm doing a lapply on the city names list (communes$commune) and then for each city I create the excel workbook with a sheet for each object name into list_files$Tables.

Even if I set colNames=TRUE (which is the default value) writeData() always remove the columns names.

I need my column names. Any idea why this happen, and how to avoid it ?

Thanks.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source