Change temporary directory
Asked Answered
B

6

44

I am using R on windows and need to change the temporary directory where tmp files are stored.

I checked a few answers, here, in R-help, etc., but no one is working.

Some links I tried: here, here, and here.

After trying those answers (I have to say that I do not get exactly the point on them), tempdir() still is the default, as much as I try different ways.

Can anybody can give a detailed example procedure of how to do this?

My session Info:

R version 2.15.2 (2012-10-26)
Platform: i386-w64-mingw32/i386 (32-bit)

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] tools_2.15.2
Barred answered 14/6, 2013 at 11:14 Comment(3)
You linked so many approaches, but only mentioned that they had no effect. Could you please give your DETAILED CODE how you tried to change the temp directory and what the error messages (if any) were.Faculty
Sorry for that, i tried so many things before reading the answer below that i did not wanted to make that big list... Anyway, Mattew Plourde pointed the perfect solution.Barred
See also : https://mcmap.net/q/389907/-changing-temporary-directory-in-r-duplicate/4137985Azelea
T
36

Create a file called .Renviron in the directory given by Sys.getenv('R_USER') and save it with the line TMP = '<your-desired-tempdir>'.

write("TMP = '<your-desired-tempdir>'", file=file.path(Sys.getenv('R_USER'), '.Renviron'))
Theiss answered 14/6, 2013 at 12:19 Comment(4)
Shouldn't the variable be called TMPDIR instead of TMP since TMPDIR has higher precedence (if set)?Faculty
That is great, Exactly the perfect solution. In your answer, i changed TMP by TMPDIR, as Beastfield said. Anyway, it works ! Many thanks !Barred
I realize this question concerns windows, but for linux users stumbling over this question: you can just put the .Renviron file into your home folder.Geulincx
To clarify, is <your-desired-tempdir> supposed to be entered verbatim, or does it indicate that I'm supposed to choose a directory myself?Pipsqueak
L
10

In windows, for me what worked is creating a file named Renviron.site and filling it with

TMPDIR=E:/rtemp 
TMP=E:/rtemp 
TEMP=E:/rtemp

Where E:/rtemp was the path to the directory where I wanted the temporary files. So you create a new text file, fill it with the above, and change its name (and extension) to Renviron.site.

Put it inside the R installation directory, in the directory etc (e.g. C:\Program Files\R\R-3.3.2\etc)

Obviously, you need to restart R studio for the change to work! (I use R studio but it should work in R also).

For me, this change allowed me to run a script of species distribution modeling which was creating very large temporary files on the system partition, consuming all the space and killing the process in the end. I have moved the temp files to a usb SSD disk (partition E:), and voila, it worked.

PS - the answer was in one of the links you mentioned.

Lactic answered 6/12, 2017 at 7:39 Comment(1)
The answer was in one of the links, but your explanation is much better! Worked for me.Odelsting
A
5

For Linux, I'm using Ubuntu 18.04.1 LTS. You can try the following line:

write("TMP = YOUR_PATH_VARIABLE", file=file.path('~/.Renviron'))

Explanation: This line will write the TMP variable, which has been assigned to your own temp path, to the '.Renviron' file. And this '.Renviron' file will be created in your home directory. If this doesn't work, restart your R or R studio. The reason is the temporary directory was created before the current R session. So you have to restart another R session to implement this new TEMP_PATH configuration.

Amphidiploid answered 12/9, 2018 at 21:5 Comment(2)
Thanks! Creating /home/user/.Renviron with TMP=/home/user/mytmp solved by problem of /tmp filling (they're on separate partitions)Alderson
Thanks! This was the only suggestion that worked for me.Thereinafter
S
2

I had a similar problem. In my case the solutions described above didn't work. Rcpp when compiling still used the tempdir(). It was driven by the fact that my default TEMPDIR was using my Windows user folder, I have Polish letters in my user name and R does not like it.

What I found is that TEMP, TMP and TMPDIR have to be set before running R: https://cran.r-project.org/web/packages/startup/vignettes/startup-intro.html

And this should be done in the system. I used following instructions: https://answers.microsoft.com/en-us/windows/forum/windows_7-files/change-location-of-temp-files-folder-to-another/19f13330-dde1-404c-aa27-a76c0b450818

But instead of changing TEMP and TMP variables I created a TMPDIR variable in Windows. And it worked for me. After restart R points to the new tempdir() as guided by Windows TMPDIR!

Slovenia answered 10/2, 2020 at 13:53 Comment(0)
F
1

The usethis package has a handy shortcut: usethis::edit_r_environ()

This will open and/or create your environment file, then you can fill in the answer from Adrian Stoica above:

TMPDIR=E:/rtemp 
TMP=E:/rtemp 
TEMP=E:/rtemp
Ferromagnetic answered 28/9, 2022 at 21:2 Comment(0)
R
0

For those who came here to facilitate R unit tests (e.g., with the testthat or tinytest package), you can change to a temporary folder for reproducible workflows using dir.create():

test_that("the functions used for folder processing work", {

  # this will create a temp folder, but tempdir() won't let you name it:
  test_dir <- tempdir() 

  # this will create a folder within our temp folder, with a name of our choice:
  test_dir <- paste0(test_dir, "/hello123")
  dir.create(path = test_dir)
  
  # new folder should exist now:
  expect_true(dir.exists(test_dir))

  # and now the unit tests:

  # let's say we have a function `has_folder_named_hello123()` in our package,
  # which checks the folder structure for a folder named 'hello123':
  expect_true(has_folder_named_hello123(test_dir))
})
Regenerate answered 3/1, 2022 at 21:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.