Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting file permissions for created files in R

Tags:

I'm on Linux and want to share R output with co-workers while also allowing them to overwrite my files. However, when I write a file the permissions are set to read-only for the group, for example:

> write.csv(data.frame(a = 1:3), file = "/tmp/test.csv")
> file.mode("/tmp/test.csv")
[1] "644"

creates a file that is only writeable by myself. Is there some option I can set so that the files I write have permission 660 set automatically for all ways of writing files (write.csv, data.table, etc)?

like image 516
Bob Jansen Avatar asked Jun 19 '17 15:06

Bob Jansen


1 Answers

The solution is to set the umask using Sys.umask as follows.

# Before setting umask as in the question:
> write.csv(data.frame(a = 1:3), file = "/tmp/test.csv")
> file.mode("/tmp/test.csv")
[1] "644"
# Setting the umask results in succes:
Sys.umask("006")
> write.csv(data.frame(a = 1:3), file = "/tmp/test2.csv")
> file.mode("/tmp/test2.csv")
[1] "660"
like image 133
Bob Jansen Avatar answered Sep 30 '22 14:09

Bob Jansen