Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

write.xlsx function gives error when defining path with the file name but read.xlsx is fine

Tags:

r

rjava

r-xlsx

I have a problem with the write.xlsx2 function of xlsx R package. For instance, see the code below.

main_path<-"~/mydir/"
read.xlsx2(paste0(main_path,"my_input_excel.xlsx"), sheetIndex=1)
a<-1
write.xlsx2(a, paste0(main_path,"my_output_excel.xlsx"), sheetName="Sheet1", col.names=TRUE, row.names=FALSE, append=FALSE)

While read.xlsx2 function works fine, write.xlsx2 function gives the error

Error in .jnew("java/io/FileOutputStream", jFile) :
java.io.FileNotFoundException: /mydir/my_output_excel.xlsx (No such file or directory)

When I remove the paste0 part and write only the file name everything is fine again. So the problem is to define path.

p.s I am wondering, perhaps the write.xlsx ignores the tilde ~ so path definition becomes garbage.

like image 638
berkorbay Avatar asked May 13 '14 14:05

berkorbay


People also ask

What package is write Xlsx in R?

openxlsx R Package provides several features and high-level interfaces to write/export, format, and edit Excel spreadsheet or worksheet. For example, use write. xlsx() to export DataFrame to excel spreadsheet in R. In order to use openxlsx library, you need to first install it by using install.


1 Answers

Replacing "~/" with "Users//" works for Mac (probably for Linux as well). Though, it still eludes me how read.xlsx and write.xlsx could differ in such a fundamental way.

like image 151
berkorbay Avatar answered Oct 12 '22 19:10

berkorbay