Skip to main content

R Studio tmp directory

Setting a new tmp directory

often when using packages like raster the /tmp/ directory will fill with temporary files. To prevent this you can set a new temporary directory in one of two ways details below

Temporarily set the tmp directory

note that the ~/tmp/ directory must exist and that this will use your home folder which may cause your home folder to fill up if you do not have enough free space. Free space in your home folder can be checked with: 

df -h ~/

if you do not have /tmp in your home folder you can create it with: 

mkdir ~/tmp
> tempdir()
[1] "/tmp/RtmpEpCIDc"
> newtmp <- "~/tmp/rcall"
> dir.create(newtmp, recursive = TRUE)
> Sys.setenv(TMPDIR = tools::file_path_as_absolute(newtmp))
> unlink(tempdir(), recursive = TRUE)
> tempdir(check=TRUE)
[1] "/root/tmp/rcall/RtmpmGFIia"
 

Permanently set the tmp directory (Recommended)

Basically, this is done by editing your .Renviron file and adding TMPDIR=/home/username/tmp 

 

Method 1: Using the R console

usethis::edit_r_environ() 

Then add this to the file:

TMPDIR=/home/your-username/tmp

(Replace /home/your-username with your actual home path, as ~ may not always be expanded correctly in .Renviron.)

Save and close the file.

Restart R (or RStudio) for the change to take effect. Verify it's working by running:

Sys.getenv("TMPDIR")

You should see:

    [1] "/home/your-username/tmp"

Method 2: Using the Terminal command line

in the terminal run (or similarly using your favorite editor):

touch $HOME/.Renviron

and enter the following:

TMPDIR=/home/your-username/tmp

You will need to quite your current R session and start a new one for it to take effect.