tags:

views:

83

answers:

2

I want to write some temporary data to disk in an R package, and I want to be sure that it can run on every OS without assuming the user has admin rights. Is there an existing R function that can provide a path to a temporary directory on all major OS's? Or a way to reference a user's home directory?

Otherwise, I was thinking of trying this:

Sys.getenv("temp")

I presume that I can't expect people to have write access to their R locations, otherwise I could reference a path within the package directory: .find.package("package.name").

A: 

After some further thought, I think that this should work:

path.expand("~")

That will give the home directory, which should have write access.

Shane
That gives the home directory, not the working directory.
hadley
I would be more than a little annoyed if someone's code that I'm running started writing temporary files in my home directory.
Ken Williams
In this case, these may be files that the user would want to keep, but I would give them the choice. This is really about having a good default setting. I'm going to use `tempdir` instead.
Shane
+8  A: 

Yes, there is: tempdir.

This will return a session specific directory within the user's temp directory. (So it gives the same value every time you call it within a specific R session. Shut R and restart, and it will give you a different directory.)

Richie Cotton
Perfect, thanks! Note to self: try apropos before asking a question: `apropos("temp")` would have returned `tempdir` and `tempfile`.
Shane