R packages requiring compilation on low memory VPS

I figured this was worth a post for the archives anyway since I didn’t find a direct solution to it via Google but eventually managed to cobble together something from various sources. Anyway, I have installed R on a VPS running Ubuntu 14.04 (as an experiment right now), but when attempting to install the dplyr package in particular by the usual method, e.g.,

sudo R
> install.packages('dplyr')

it would crash with the following error message:

g++: internal compiler error: Killed (program cc1plus)
Please submit a full bug report,
with preprocessed source if appropriate.
See <file:///usr/share/doc/gcc-4.8/README.Bugs> for instructions.
make: *** [dplyr.o] Error 4
ERROR: compilation failed for package ‘dplyr’

Some Googling later and the cause is apparently not enough memory to compile the package – 1 GB RAM and 256 MB swap on this particular VPS. I found a number of pages suggesting that this can be worked around by instead running the following:

CXXFLAGS="-g -O2 --param ggc-min-expand=0  --param ggc-min-heapsize=8192" sudo su - -c "R -e \"install.packages('dplyr', repos='http://cran.rstudio.com/')\""

…but this still caused the compiler to quit with the same error. In the end I fixed it by temporarily adding a larger swap file to the system, by first creating it and setting the permissions with:

sudo fallocate -l 1G /swapfile
sudo chmod 600 /swapfile

then telling the system to use it as swap space:

sudo mkswap /swapfile
sudo swapon /swapfile

sudo swapon -s
Filename        Type        Size    Used    Priority
/dev/xvdb       partition   262140  258732  -1
/swapfile       file        1048572 0   -2

Now, with the same command as before:

CXXFLAGS="-g -O2 --param ggc-min-expand=0  --param ggc-min-heapsize=8192" sudo su - -c "R -e \"install.packages('dplyr', repos='http://cran.rstudio.com/')\""

…and…

* DONE (dplyr)

Sorted.