Overriding InitTempDir
It is conventional for people proposing changes to R to set out a compelling case as to why they are needed. I have not seen any case here. Can we please have one? Someone will have to think through the ramifications of such a change (and BTW using edit() in an R console is not at all unlikely), and continue to maintain it for years to come. I have to say that in the past I have accepted quite a few user suggestions that became maintenance nightmares, so I have become very wary. It's not really a good time: R-devel is about to start solidifying towards 2.4.0, and we have quite a few issues to work through (e.g. the internal changes to S4 methods).
On Fri, 1 Sep 2006, Jeffrey Horner wrote:
Jeffrey Horner wrote:
For embedded projects, one may want to eliminate the per-session temp
directory created by InitTempDir() and just use a system-specific temp
directory. Here's my solution:
extern char *R_TempDir;
void my_InitTempDir()
{
char *tmp;
if (R_TempDir){
if (rmdir(R_TempDir) != 0){
perror("Fatal Error: could not remove R's TempDir!");
exit(1);
}
}
tmp = getenv("TMPDIR");
if (tmp == NULL) {
tmp = getenv("TMP");
if (tmp == NULL) {
tmp = getenv("TEMP");
if (tmp == NULL)
tmp = "/tmp";
}
}
R_TempDir=tmp;
if (setenv("R_SESSION_TMPDIR",tmp,1) != 0){
perror("Fatal Error: couldn't set/replace R_SESSION_TMPDIR!");
exit(1);
}
}
This function is called after Rf_initEmbeddedR; it's seems like a hack
but it works. Does anyone have other solutions?
I just found a fault with the above solution, but only if a program that embeds R calls the R edit function; highly unlikely. Nevertheless, It turns out that soon after InitTempDir is called, InitEd is called and sets the variable DefaultFileName (which is hidden by storage class static, unlike R_TempDir) to a temporary filename located in the directory which was just deleted by my_initTempDir.
Maybe InitTempDir could check if R_TempDir was not NULL and then just return.
So it would be nice if the above (InitTempDir only creating a directory
when R_TempDir is NULL) could be added to R-trunk.
Note also that in the case that R_TempDir is set to a system-specific
temp directory, the program would then have to set it back to NULL as
the new function Rf_endEmbeddedR now tests to see if R_TempDir has been set:
/* use fatal !=0 for emergency bail out */
void Rf_endEmbeddedR(int fatal)
{
unsigned char buf[1024];
char * tmpdir;
R_RunExitFinalizers();
CleanEd();
if(!fatal) KillAllDevices();
if((tmpdir = R_TempDir)) {
snprintf((char *)buf, 1024, "rm -rf %s", tmpdir);
R_system((char *)buf);
}
if(!fatal && R_CollectWarnings)
PrintWarnings(); /* from device close and .Last */
fpu_setup(FALSE);
}
Jeff
Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595