Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

write.xlsx does not error if it doesn't have write permissions #190

Closed
jeff-m-sullivan opened this issue May 11, 2021 · 3 comments · Fixed by #208
Closed

write.xlsx does not error if it doesn't have write permissions #190

jeff-m-sullivan opened this issue May 11, 2021 · 3 comments · Fixed by #208
Labels
question Further information is requested waiting for answer If not answered, the issue will be closed in 7 days.

Comments

@jeff-m-sullivan
Copy link
Contributor

Describe the bug
If write.xlsx is asked to write out data to a file that the user doesn't have write permissions to, it fails silently.

To Reproduce
Steps to reproduce the behavior:

> fs::file_create("foo.xlsx", mode="a=rx")
> openxlsx::write.xlsx(x=cars, file="foo.xlsx", overwrite=TRUE)

Expected behavior
It should throw an error, like write.csv does:

> fs::file_create("foo.csv", mode="a=rx")
> write.csv(cars, file="foo.csv")
Error in file(file, ifelse(append, "a", "w")) : 
  cannot open the connection
In addition: Warning message:
In file(file, ifelse(append, "a", "w")) :
  cannot open file 'foo.csv': Permission denied

This was using the latest CRAN version: 4.2.3

@ycphs
Copy link
Owner

ycphs commented May 11, 2021

Please try the parameter returnValue

Additional parameters

@ycphs ycphs added question Further information is requested waiting for answer If not answered, the issue will be closed in 7 days. labels May 11, 2021
@jeff-m-sullivan
Copy link
Contributor Author

Thanks for the suggestion, but it yields the same output whether or not the write operation is successful:

> fs::file_create("foo.xlsx", mode="a=rx")
> (openxlsx::write.xlsx(x=cars, file="foo.xlsx", overwrite=TRUE, returnValue=TRUE))
A Workbook object.
 
Worksheets:
 Sheet 1: "Sheet 1"
 

 
 Worksheet write order: 1> 
> (openxlsx::write.xlsx(x=cars, file="foo2.xlsx", overwrite=TRUE, returnValue=TRUE))
A Workbook object.
 
Worksheets:
 Sheet 1: "Sheet 1"
 

 
 Worksheet write order: 1> 

Here are the two files:

Downloads % ls -l foo*
-r-xr-xr-x  1 localjeff  staff     0 May 11 14:53 foo.xlsx
-rw-r--r--  1 localjeff  staff  6742 May 11 14:54 foo2.xlsx

@jmbarbone
Copy link
Contributor

write.xlsx() doesn't seem to use the returnValue parameter for saveWorkbook(). There would also be a bit of a weird value return since write.xlsx() also returns the Workbook object (this could be changed after #194 since generating the Workbook could be done separately from saving).

This is an issue with saveWorkbook():

https://github.com/ycphs/openxlsx/blob/master/R/wrappers.R#L122-L134

There's a tryCatch() wrapped around the file.copy(). I don't know if that's necessary. I would expect that if the file doesn't copy (ie, save) then the function should fail.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested waiting for answer If not answered, the issue will be closed in 7 days.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants