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")
.
When you start a new R session a temporary directory is created. Location of the directory depends on your system and configuration. Function tempdir() returns path to the current temporary directory. On normal exit R cleans up this directory, so don't put there nonreproducible data.
Temporary File Paths In contrast to tempdir() , the function tempfile() returns a file relative to a specific directory. That is, the following code will return a path for a generic file within the temporary directory.
It returns a temporary directory created at the beginning of the session, and is where new temporary files created by tempfile() are stored by default. However it's not related to the working directory. If you want to change the working directory you should use the command setwd() .
Temporary files are automatically removed at the end of each R session that ends normally. You can use tmpFiles to see the files in the current sessions, including those that are orphaned (not connect to a SpatRaster object any more) and from other (perhaps old) sessions, and remove all the temporary files.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With