# Installing INLA in R
[Downloading INLA](http://www.r-inla.org/download)
INLA has many dependencies; two (`MCMCpack`, `sf`) have particular problems. Before installing INLA in R, install these two packages separately.
In general, I have these modules loaded:
```
$ ml
Currently Loaded Modules:
1) intel/18.0.2 2) cxx17/7.3.0 3) R/3.5.1-test
```
## MCMCpack
`MCMCpack` seems not to build properly with the Intel compilers that are necessarily loaded in order to load R. So use a `Makevars` file to tell R to use Gnu compilers. I'm not sure, but it seems like a running instance of R doesn't pick up changes in the `Makevars` file, so I just quit R, edit `Makevars`, and restart R.
### Create `Makevars` file
In `~/.R` create a file named `Makevars`. You may have to create the directory `~/.R`.
```
## Use gcc, g++ instead of Intel compilers
CC=gcc
CXX= g++
## gcc, g++ use -fopenmp, not -qopenmp
CFLAGS= -fopenmp
CXXFLAGS= -fopenmp
```
### Install `MCMCpack` package in R
On the head node, start R:
```
$ R
```
In R, install `MCMCpack` (responding to prompts):
```
> install.packages("MCMCpack")
```
Then quit R so we don't get confused with this `Makevars` later.
### Clean up `Makevars`
Anytime R builds anything, it looks for `~/.R/Makevars`. So after you install a package with the `Makevars` file that it needs, you need to get rid of `Makevars` to install subsequent packages (I forget to do this all the time). As a way of documenting what options various packages need and to minimize reinventing the wheel, I save `Makevars` files with descriptive extensions. So
```
$ mv ~/.R/Makevars ~/.R/Makevars.gcc
```
both gets `Makevars` out of the way for later installs and keeps a `Makevars.gcc` file around that can be copied to `Makevars` later to force use of the Gnu C/C++ compilers.
## sf
Through trial and error, there are two additional things to do to install the `sf` package. You need to load the `gdal` module before starting R, and you need to add options to the `install.packages()` command in R.
```
$ ml gdal
$ R
> install.packages("sf", configure.args=c("--with-proj-share=/usr/local/pgc/gnu/4.8.5/2018/proj/share/proj", "--with-geos-config=/usr/local/pgc/gnu/4.8.5/2018/geos/bin/geos-config"))
```
I do get some unusual messages at the end but `sf` seems to install:
```
installing to /home/shew.1/R/x86_64-pc-linux-gnu-library/3.5/sf/libs
** R
** demo
** inst
** byte-compile and prepare package for lazy loading
in method for ‘dbWriteTable’ with signature ‘"PostgreSQLConnection","character","sf"’: no definition for class “PostgreSQLConnection”
in method for ‘dbDataType’ with signature ‘"PostgreSQLConnection","sf"’: no definition for class “PostgreSQLConnection”
in method for ‘coerce’ with signature ‘"Spatial","sf"’: no definition for class “Spatial”
in method for ‘coerce’ with signature ‘"Spatial","sfc"’: no definition for class “Spatial”
in method for ‘coerce’ with signature ‘"sf","Spatial"’: no definition for class “Spatial”
in method for ‘coerce’ with signature ‘"sfc","Spatial"’: no definition for class “Spatial”
in method for ‘coerce’ with signature ‘"XY","Spatial"’: no definition for class “Spatial”
** help
*** installing help indices
*** copying figures
** building package indices
** installing vignettes
** testing if installed package can be loaded
* DONE (sf)
The downloaded source packages are in
‘/tmp/RtmpJzrQ4p/downloaded_packages’
```
## INLA
Finally, install INLA and its remaining dependencies:
```
> install.packages("INLA", repos=c(getOption("repos"), INLA="https://inla.r-inla-download.org/R/stable"), dep=TRUE)
```