Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R writing excel file with XLSX package taking a long time and error

Tags:

r

The "mydata" data frame has 128,000 rows. When I write it to an xlsx file it just keep running and when I stop it I get the error:

write.xlsx(x = mydata, file = "myfile.xlsx",
           sheetName = "Sheet1", row.names = FALSE)





Error in .jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", cl,  : 
  java.lang.OutOfMemoryError: Java heap space

Do you know what this means?

like image 812
user3022875 Avatar asked Dec 11 '22 02:12

user3022875


1 Answers

I advise you not to use those libraries (write.xlsx2 & write.xlsx), indeed both are suffering about data size and a lot of format construction. The simplest way to write a dataframe (I suppose your original data is in a dataframe structure or it's possible to transform them to a dataframe) is using writexl library. Here there is an example:

library(writexl)

df <- data.frame(name = c("Jon", "Bill", "Maria"),
                 age = c(23,41,32))

xlx_store_path <- "where do you want to store the .xlsx file"

write_xlsx(df, xlx_store_path)

Complete suorce here: https://datatofish.com/export-dataframe-to-excel-in-r/

like image 138
Hamza Avatar answered Apr 30 '23 23:04

Hamza