How do I password-protect an Excel file created in R with write.xlsx?
Asked Answered
D

1

7

In my data analysis in R I sometimes output data to Excel files using write.xlsx. These files need to be password-protected, and currently I am doing this by opening the unprotected file using Excel, and then adding a password using File - Protect Workbook - Encrypt with Password.

Is there a way to do this directly in R so that I don't have to open the Excel file and manually add the password? (I want to password-protect the whole workbook, not just a single sheet.)

Dejadeject answered 15/6, 2018 at 0:22 Comment(3)
You could try out this post: #37991889Psittacosis
The (now deleted) answer from @Psittacosis suggested using the password argument to write.xlsx. However, you said that you got an unused argument error when you tried this. You might just need to update the package. The latest version of xlsx is 0.6.1, released on June 7, 2018, in which write.xlsx has a password argument. I happen to have xlsx version 0.5.7 installed at the moment and there's no password argument in that version.Androgen
@Androgen Thanks for the heads-up (I un-deleted my answer). After re-installing the xlsx package, indeed write.xlsx has the password argument.Psittacosis
P
8

From the xlsx manual, you can set the password in the password argument:

write.xlsx(x, file, sheetName="Sheet1", col.names=TRUE, row.names=TRUE, append=FALSE, showNA=TRUE, password=NULL)
Psittacosis answered 15/6, 2018 at 0:32 Comment(2)
I just tried this, by setting a password character string PASS, and it came back with an error message - Error in write.xlsx(... : unused argument (password = PASS).Dejadeject
@Dejadeject You might want to try re-installing the xlsx package. The latest update for the package has the password argument for write.xlsx.Psittacosis

© 2022 - 2024 — McMap. All rights reserved.