crcran

Change tempdir() in session (update R_TempDir)


I am looking for a way to change the tempdir() location after an R session has started. I think it would be required to update the C level global variable R_TempDir. What would be a nice way to do this from within R?


Solution

  • Update: Simon Urbanecks unixtools package has a function to accomplish this. Below the code (for future reference).

    set.tempdir <- function(path) {
      invisible(.Call(C_setTempDir, path.expand(path)))
    }
    

    C code:

    #include <string.h>
    #include <Rinternals.h>
    #include <Rembedded.h>
    
    SEXP C_setTempDir(SEXP sName) {
        if (TYPEOF(sName) != STRSXP || LENGTH(sName) != 1)
        Rf_error("invalid path");
        R_TempDir = strdup(CHAR(STRING_ELT(sName, 0)));
        return sName;
    }