Error exporting dataframe using writexl package

Hi, i found about a package who could export my data.frame to excel, but i encounter some difficulties.
(I'm new on this plateform and i'm sorry if something in my request is wrong, do not hesitate to get me noticed about it if it is the case!)

I am trying to export a dataframe called "div1" to an excel doc called "TEST.xlsx" using the function write_xlsx from the package writexl, but i got an error message and don't know how to resolve it:

There is my running of the console:

> write_xlsx(div1,"C:\\Users\\Utilisateur\\Desktop\\TEST.xlsx")
[ERROR] workbook_close(): Error creating 'C:\Users\Utilisateur\Desktop\TEST.xlsx'. System error = Permission denied
Erreur : Error in libxlsxwriter: 'Error creating output xlsx file. Usually a permissions error.'

PS: i do not know why but the topic creation transformed my double "" into a single one

I'm using the Rstudio Version 1.3.1093 in admin mode and using doc.xlsx with OpenDocument.

Thanks for your help,

The error says that the function has encountered a permission problem. Are you logged into Windows with the user name Utilisateur? Have you tried saving the data frame without specifying a path so that it safes to a temp folder? That would prove that you can save the file somewhere.

Yes i am logged with Utilisateur.
I do not understand what you mean by "specific path", neither about saving to a temp folder.. (you mean on the cloud or something like that?)
Sorry for my poor knowledge, make the thing more complicated for you...

The documentation for the writexl function includes this summary of its arguments

write_xlsx(x,path = tempfile(fileext = ".xlsx"),col_names = TRUE, 
           format_headers = TRUE,use_zip64 = FALSE)

The argument x is the data frame you are saving, which is div1 in your case. You have given the path argument as

"C:\\Users\\Utilisateur\\Desktop\\TEST.xlsx"

but if you do not provide it, the function will use the result of

tempfile(fileext = ".xlsx")

That produces a random file name with the .xlsx extension. I just ran
tempfile(fileext = ".xlsx") on my system with the following result.

tempfile(fileext = ".xlsx")
[1] "C:\\Users\\fjcc\\AppData\\Local\\Temp\\Rtmp6RY7Qz\\file5cd45e13857.xlsx"

so it provides a file as a sub folder of my AppData folder. If you run

writexl(div1)

do you get an error?

Super! It worked !!

If I run this:

tempfile(fileext = "C:\Users\Utilisateur\Desktop\TEST.xlsx")
[1] "C:\Users\UTILIS~1\AppData\Local\Temp\RtmpEPUWRC\file303427cd118aC:\Users\Utilisateur\Desktop\TEST.xlsx"
write_xlsx(d)

I get to found my .xlsx doc into that file "RtmpEPUWRC".

Thats already really nice, but is there a way to transfer my dataframe into an already created .xlsx doc, as i excepted earlier with my doc "TEST.xlsx" ?

I did not understand that the file TEST.xlsx already exists on your desktop. I don't think you can write into that file using the writexl package. I think you can do tis with the openxlsx package, though I have never tried it. Do you want to write your data frame into a new sheet in the existing file, or do you want to append the data to data that are already there, or do you want to overwrite the data on an existing sheet?

I will have to step away from this thread for several hours but someone else may well be able to help you once you explain how you want to modify the existing file.

I wanted to created new sheet into that file already created, yes. But that doesnt really matter anymore, the temp file will do just fine I guess..
But if someone has a simpler way to do it (and directly to a precise file), you can do it of course.

Thanks a lot for your help, I really appreciate it !

Have a good day

To write a new sheet into an existing file, you can use code like this.

library(openxlsx)
wb <- loadWorkbook("Data.xlsx")
addWorksheet(wb = wb, sheetName = "MyNewSheet")
DF <- data.frame(X = 1:4, VALUE = 2:5)
writeData(wb = wb, sheet = "MyNewSheet", x = DF, startCol = 1, startRow = 1)
saveWorkbook(wb = wb, file = "Data.xlsx", overwrite = TRUE)

This is not working..

Error in loadWorkbook("TEST.xlxs") : File does not exist.

Even if, it is, created (on my desktop)

But don't worry, as I said earlier, the temporary files will do just fine.

Thanks agin a lot, have a good day !

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.