Tutorial :Writing temporary data from R



Question:

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").


Solution:1

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.)

pathological::temp_dir provides a more user friendly wrapper.


Solution:2

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

path.expand("~")  

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


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »