1

I have 15 tibbles that I want to export to a single XLSX workbook, with the sheetName for each set to be the same as the name of the tibble object. To export a single tibble, this works just fine:

library(xlsx)

    my_tibble1 %>% 
      write.xlsx("output_filename.xlsx", 
                 sheetName = "my_tibble1", 
                 append = TRUE)

However, there are enough of these tibbles that writing all that out for each one is time-consuming. So, I wrote a function:

output_expediter <- function(df, output_filename) {
      write.xlsx(df, 
                 output_filename, 
                 sheetName = deparse(substitute(df)), 
                 append = TRUE)

This function successfully writes the tibble to a new sheet in the output workbook, BUT the sheetName is always a single period (".").

All the variable names used for the tibbles are limited to lowercase characters and underscores, and all of them are 31 or fewer characters long, so I don't think any of them violate XLSX format conventions. In the R console, running:

deparse(substitute(my_tibble1))

yields "my_tibble1" as expected.

Any ideas for why this is happening? Any possible workarounds, other than just typing out the names of each sheet?

Kevin Troy
  • 412
  • 4
  • 13
  • Are you applying this on a `lapply` loop – akrun Aug 01 '19 at 19:53
  • @akrun I would like to do that, if I can get the function to output with the sheetName as desired. As it stands, applying the function to a list/vector of more than one tibble will raise the error "The workbook already contains a sheet of this name" (because write.xlsx will not write over an already-existing sheet if append = TRUE). – Kevin Troy Aug 01 '19 at 20:08
  • Have you tried with openxlsx – akrun Aug 01 '19 at 20:28
  • @akrun not yet, I'll give it a try – Kevin Troy Aug 01 '19 at 20:32

1 Answers1

0

An option is openxlsx

library(openxlsx)
library(tibble)
output_expediter <- function(df, output_filename) {
     nm1 <- deparse(substitute(df))
     wb <- createWorkbook()
     addWorksheet(wb, sheetName = nm1)
     writeData(wb, sheet= nm1, x = df)
     saveWorkbook(wb =wb, file = output_filename, overwrite = TRUE)
     }


file1 <- "hello.xlsx"
df1 <- tibble(col1 = 1:5, col2 = 6:10)
output_expediter(df1, file1)

-output

enter image description here


If we need to write muliple files,

library(purrr)    
wb1 <- createWorkbook()
output_expediter <- function(df, wb, nm1, output_filename) {


     addWorksheet(wb, sheetName = nm1)
     writeData(wb, sheet= nm1, x = df)
     saveWorkbook(wb =wb, file = output_filename, overwrite = TRUE)
     }

lst1 <- lst(df1= df1, df2= df1)
iwalk(lst1, ~ output_expediter(.x, wb = wb1, .y, file))

enter image description here

akrun
  • 874,273
  • 37
  • 540
  • 662
  • Again, this yields a worksheet named "." in the workbook. If I pass the name as a string (rather than using deparse(substitute())), it works correctly. – Kevin Troy Aug 01 '19 at 21:00
  • Thanks. The iwalk raises "Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : arguments imply differing number of rows: [list of the numbers of rows in each of the various tibbles]". – Kevin Troy Aug 02 '19 at 03:47
  • @KevinTroy. Sorry, it is working fine for me with. the data showed. i used `packageVersion("purrr")# [1] ‘0.3.2’` – akrun Aug 02 '19 at 13:14