I'm experiencing an issue when compiling R packages containing c++ code
inside a sandbox on linux. The problem is caused by the fact that g++ does
not seem to inherit the TMPDIR environment variable from the R process.
When creating the shared on library, it tries to write a file under /tmp
(instead of $TMPDIR). If /tmp is not writable, this causes the package
build to fail.

I am personally using RAppArmor to enforce security policies, but an easier
way to reproduce the problem is by temporarily disable write access for
non-root users to /tmp. Something like this (run as non-root):

#disable default tmp location
sudo chmod 755 /tmp

#run R with custom tempdir
mkdir ~/mytmp
TMPDIR=~/mytmp R

#in R:
tempdir() #verify
Sys.getenv("TMPDIR") #doublecheck
install.packages("RcppExamples") #or any other c++ package


The compiling seems to be OK, but when creating the shared library g++
throws an error (see below). When using apparmor, it actually gives a more
detailed error stating that the process tried to create a file
/tmp/tmpfDbJ9KD, which was denied.

g++ -I/usr/share/R/include -DNDEBUG
-I"/home/jeroen/R/x86_64-pc-linux-gnu-library/3.0/Rcpp/include"   -fpic
 -O3 -pipe  -g  -c newRcppStringVectorExample.cpp -o
newRcppStringVectorExample.o
g++ -I/usr/share/R/include -DNDEBUG
-I"/home/jeroen/R/x86_64-pc-linux-gnu-library/3.0/Rcpp/include"   -fpic
 -O3 -pipe  -g  -c newRcppVectorExample.cpp -o newRcppVectorExample.o
g++ -shared -o RcppExamples.so RcppDataFrame.o RcppRNGs.o
newRcppDateExample.o newRcppMatrixExample.o newRcppParamsExample.o
newRcppStringVectorExample.o newRcppVectorExample.o Fatal error: creating
temporary file for '-e' failed -L/usr/lib/R/lib -lR
g++: error: Fatal: No such file or directory

I get the same error for every package that depends on Rcpp. For some
reason it only happens for g++, I haven't seen the problem yet for gcc.

Is it correct to assume that g++ is supposed to inherit the TMPDIR
environment variable from the R process when called via install.packages?
Or does TMPDIR somewhere get overridden? Is there a way to use
install.packages such that it calls g++ with environment variable TMPDIR
equal to Sys.getenv("TMPDIR") from the current process?

> sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] Rcpp_0.10.6


jeroen@Ubuntu:~$ g++ --version
g++ (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

	[[alternative HTML version deleted]]

