[R] Set temporary folder for R in Rstudio server

[This article was first published on R on Zhenguo Zhang's Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Zhenguo Zhang’s Blog /2024/07/26/r-set-temporary-folder-for-r-in-rstudio-server/ –

In another post, I described how to install Rstudio server on Ubuntu. In this post, I will describe how to set temporary folders for R sessions connected to an Rstudio server. This is important, because the default temporary folder /tmp could get overload easily when multiple R sessions are connected and/or big data are stored, yielding lack of space issue.

Temporary folder in R

One can get the temporary directory set for the current R session by typing the following command in R terminal:

1
tempdir()

The temporary directory was set by reading the following environment variables in order until finding one pointing to a writable directory:

  • TMPDIR
  • TMP
  • TEMP

If none is found, the temporary folder will be set to /tmp in Linux.

So to change the tempoary folder of R sessions, one need to set one of these variables, ideally TMPDIR.

A bit introduction on R environment variable setting

When Rstudio launches R sessions, it does it under a bash login shell, so before starting R, it reads configurations from the file /etc/profile if it exists, and then first of the following:

  • ~/.bash_profile
  • ~/.bash_login
  • ~/.profile

Therefore you can set up environment variables in any of these files.

Also, you can set environment variables in the following files:

  • ${R_HOME}/etc/Renviron: affect all users
  • ~/.Renviron: only impact the user whose home folder is changed

If you use Posit Workbench, you can also set environment variables in the file /etc/rstudio/rsession-profile, check this post. Note this won’t work for free Rstudio server.

The solution

You can add the following line into the file ~/.Renviron to change temporary folders.

1
TMPDIR=/path/to/directory

You can also modify any of the other above-mentiond files to set the environment variables.

Don’t forget to restart R or Rstudio server for changes to take effect.

Then have a peace in data analysis, especially big data.

References

– /2024/07/26/r-set-temporary-folder-for-r-in-rstudio-server/ –
To leave a comment for the author, please follow the link and comment on their blog: R on Zhenguo Zhang's Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)