Run and walk away from the terminal with:

nohup Rscript analyse.R > output-random-forest 2>&1 &

Set up more swap memory (7Gb in this case):

sudo dd if=/dev/zero of=/swapfile.img bs=1024 count=7M
sudo mkswap /swapfile.img
sudo swapon /swapfile.img

In this case the memory is instantly available but not on reboot, unless added to fstab with this line:

/swapfile.img swap swap sw 0 0