Zhenguo Zhang's Blog
Sharing makes life better
[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


Last modified on 2024-07-26

Comments powered by Disqus