I am looking for a way to change the tempdir()
location after an R session has started. I think it would be required to update the C level global variable R_TempDir
. What would be a nice way to do this from within R?

- 16,027
- 21
- 69
- 98

- 31,998
- 35
- 134
- 207
5 Answers
Update: Simon Urbanecks unixtools package has a function to accomplish this. Below the code (for future reference).
set.tempdir <- function(path) {
invisible(.Call(C_setTempDir, path.expand(path)))
}
C code:
#include <string.h>
#include <Rinternals.h>
#include <Rembedded.h>
SEXP C_setTempDir(SEXP sName) {
if (TYPEOF(sName) != STRSXP || LENGTH(sName) != 1)
Rf_error("invalid path");
R_TempDir = strdup(CHAR(STRING_ELT(sName, 0)));
return sName;
}

- 31,998
- 35
- 134
- 207
-
1That's totally right! `install.packages('unixtools', repos = 'http://www.rforge.net/'); unixtools::set.tempdir("/new/tmp/path")` did the trick. – fdetsch Jan 27 '17 at 07:53
If you unlock tempdir()
and re-assign a new function to the baseenv()
it might work:
tempdir <- function() "/NewTempDir"
unlockBinding("tempdir", baseenv())
assignInNamespace("tempdir", tempdir, ns="base", envir=baseenv())
assign("tempdir", tempdir, baseenv())
lockBinding("tempdir", baseenv())

- 81
- 1
- 3
It's awfully cheesy, but you could just mask base::tempdir
by saying
tempdir <- function() { "[desired temp dir here]" }
Then you'd be OK as long as you weren't using code that (implicitly or explicitly) looked in the base namespace before the global environment ...
I really don't see any other way to do this, since it's set at initialization time and not altered thereafter. In other words, Sys.setenv(TMPDIR="/home/bolker/R")
doesn't work -- it's too late (as you probably know).
If tempdir()
were less hard-coded it would be a lot easier ... I don't really understand the design criteria here (or, less charitably, whether there were carefully thought-out design criteria ...). (I feel similarly grumpy about the hard-coding/design of .libPaths()
, which is similar ... no way to change things once you're in a running R session.)

- 211,554
- 25
- 370
- 453
-
This is not going to work. Most of the functions calling tempdir are actually inside the base package themselves. I really need something that modifies the C level global. – Jeroen Ooms Apr 25 '12 at 22:12
-
ps: .libPaths() is actually not that hardcoded. Here is a code snippet to set them: http://pastebin.com/E2sVKr30 – Jeroen Ooms Apr 25 '12 at 22:13
-
1I don't think you can. I think R treats that as a 'constant' once set. – Dirk Eddelbuettel Apr 25 '12 at 22:42
Year 2016, still haven't seen a good answer for my taste and use case. I faced the same problem and found the following solution is the cleanest from users' point of view:
# test.R content
message(tempdir())
Now change root tempdir at runtime of a script:
mkdir rtmp; TMPDIR=$PWD/rtmp Rscript test.R; rm -rf rtmp
Or at runtime of an expression:
mkdir rtmp; TMPDIR=$PWD/rtmp R -e 'tempdir()'; rm -rf rtmp
It is also worth to mention that a programmer who is writing a script/package that use a lot of space for temp files should handle tmpdir the way @João Daniel mentioned.

- 5,675
- 8
- 38
- 50
-
1Year 2017, it works perfectly, i don't want to install any other package so i go for this solution and it works thantks !!! – HanniBaL90 Apr 27 '17 at 23:18
The tempdir()
function is a nice way to create a temporary directory so you can store files valid during the last of the session. 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()
.
If you want to change the directory where temporary files are stored, you should set this as a parameter on the command.
tempfile(tmpdir="/my/path/to/tmpfile")

- 8,696
- 11
- 41
- 65
-
I know this. I explicitly want to change the default behavior of tempdir() and tempfile(); – Jeroen Ooms Apr 25 '12 at 20:12
-
Is something related to this? http://tolstoy.newcastle.edu.au/R/e2/devel/06/09/0025.html I could override some System environment variables, but it don't seem to modify `tempdir()` return. – João Daniel Apr 25 '12 at 20:19