Reading a TMP dir system in R

What is a cross-platform method for finding a temporary OS directory from R? I am currently using:

dirname(tempdir()) 

What work was done both on Ubuntu and on Windows from an interactive session R. However, then this failed when calling from inside RApache. In RApache, the value of tempdir() always /tmp , so dirname(tempdir()) results in / , which is obviously not true. I also tried:

 Sys.getenv("TMP") Sys.getenv("TEMP") Sys.getenv("TMPDIR") 

as suggested ?"environment variables" , but none of them were installed on Ubuntu. It also does not seem to be installed in any of the files in /etc/R/* , so I do not quite understand how R detects this value.

+6
source share
1 answer

The environment variables "TMPDIR", "TMP", and "TEMP" can be used to change the value returned by tempdir() if the variable C R_TempDir not set (although I'm not sure how it is ready). If you need a cross-platform function that returns the path to a sensible tmp directory and is not interested in the value of R_TempDir , you can use something like this:

 gettmpdir <- function() { tm <- Sys.getenv(c('TMPDIR', 'TMP', 'TEMP')) d <- which(file.info(tm)$isdir & file.access(tm, 2) == 0) if (length(d) > 0) tm[[d[1]]] else if (.Platform$OS.type == 'windows') Sys.getenv('R_USER') else '/tmp' } 

This is based on the InitTempDir function in the src / main / sysutils.c file from the source R, translated from C to R.

+1
source

Source: https://habr.com/ru/post/944678/


All Articles