Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing temporary data from R

Tags:

r

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

like image 440
Shane Avatar asked Mar 25 '10 14:03

Shane


People also ask

Where does r save temporary files?

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.

What does Tempfile do in R?

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.

How do I change Tempdir in R?

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

How do I delete temp files in R?

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.


Video Answer


1 Answers

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.

like image 118
Richie Cotton Avatar answered Sep 21 '22 15:09

Richie Cotton