Parag Gupta
Parag Gupta

Reputation: 51

Exporting multiple data frames from list into an excel file with name of tabs as different data frames in the list

enter image description here

"Data Structure in Image"

Problem in RStudio

I have a list of 33 data frames:

growth_in list[33] List of length 33

18005 list122 x 16 tibble 176row & 16col

18013 list122 x 16 tibble 176row & 16col

18019 list122 x 16 tibble 176row & 16col

18025 list122 x 16 tibble 176row & 16col

..... and so on and so forth till 33 dataframes(in total)

I am able to export multiple CSV files but I need one excel file with different table names(i.e one excel file with 33 sheets). Facing the below-mentioned error message after using excel_export command.

excel_export(growth_in, file = "Indiana.xlsx", table_names = paste0(names(growth_in)), row.names = FALSE )

Error in .jcall(cell, "V", "setCellValue", value) : method setCellValue with signature ([Ljava/lang/String;)V not found In addition: Warning message: In if (is.na(value)) { : the condition has length > 1 and only the first element will be used

Upvotes: 0

Views: 167

Answers (2)

Ricecakes
Ricecakes

Reputation: 781

You can also do this using the package openxlsx

library(openxlsx)
library(huxtable)

growth_in= list(iris1=iris[1:10,],iris2=mtcars[1:10,],iris3=iris[10:50,])
wb <- createWorkbook()

for(nm in names(growth_in)){

  df <- as_huxtable(growth_in[[nm]],add_colnames = T)
  ##you can format your table using huxtable options


  as_Workbook(df,Workbook = wb,sheet=nm)

}

openxlsx::saveWorkbook(wb,file = "Indiana.xlsx",overwrite = T)


I like this solution because it allows you to do some more formatting or processing for each sheet if needed.

Upvotes: 1

Parag Gupta
Parag Gupta

Reputation: 51

I used "writexl" package and used the following code to get the desired result.

growth_in %>% write_xlsx(path = "Indiana.xlsx")

Upvotes: 1

Related Questions