set location of temp files in R

hi I am trying to set location of temp files and tried this:

write("TMP = 'C:/users/lil/Rcode/Temp File'", file=file.path(Sys.getenv('R_USER'), '.Renviron'))

however when I run, I do not see anything... I see a .Renviron in the file path set under file = .... but I dont see any temp files. how do I get all temp files in a certain directory? thank u!!!


I dont get the directory I tried to change to..

As a result of executing your write statement I expect that the contents of your .Renviron file is

TMP = 'C:/users/lil/Rcode/Temp File'

The next time you restart R the environment variable TMP points to the indicated folder.
Each time R is started it will use a subfolder of the indicated folder for temporary files.
Which folder that is can be seen by using tempdir().
At least that is my understanding. If it does not work in this way I would check the following

  • be sure that the indicated folder already exists
  • does the .Renviron file contains the correct statement
  • duplicate the statement and replace in the second statement TMP by TEMP
    (never sure which one would work)
  • use the standard Windows backslashes (duplicate them in R statements like the above write)
  • remove the single quotes around the indicated folder (why would Windows need them?)

If this all does not work then ask an expert or read (once again?)

Good luck

thank u but I restarted R and using


I dont get the directory I set in.

the indicated folder exists

the .Renviron file contains the correct statement.

I tried TEMP instead of TMP

What are standard Windows Backslashes? I am using a windows computer

Standard I see

> Sys.getenv("temp")
[1] "C:\\Users\\Han\\AppData\\Local\\Temp"

When I included manually in my .Renviron file in C:\Users\Han\Documents the lines


I see (after restarting R in RStudio)

> Sys.getenv("tmp") 
[1] "D:\\Data\\magweg\\Temp"
> tempdir() 
[1] "D:\\Data\\magweg\\Temp\\RtmpCYX33S"

So it looks like it works for me as described.
The only thing that amazes me that I had to duplicate the backslashes in .Renviron otherwise they disappear. Apparently the .Renviron file is handled as an R file with text strings (??)

1 Like

ur supposed to use double back slash?? oo let me try!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.