# Installing Rsymphony solver package on Mac

### Introduction

Rsymphony is one of solver packages supported by TestDesign. As of Rsymphony version 0.1.29, Mac users may encounter error messages as below when trying to install the package from R.

    * installing *source* package ‘Rsymphony’ ...
    ** package ‘Rsymphony’ successfully unpacked and MD5 sums checked
    ** using staged installation
    Cannot find SYMPHONY libraries and headers.
    See <https://projects.coin-or.org/SYMPHONY>.
    ERROR: configuration failed for package ‘Rsymphony’

This document describes a potential fix to this problem. An admin account is required.

### Install SYMPHONY libraries

The cause of the error is that Rsymphony requires SYMPHONY libraries, but the libraries are not properly installed. The goal of this section is to install the libraries.

Step 1. Open the terminal from Launchpad. Switch to the admin account on your system by running the following line in the terminal prompt. Make sure to replace `username` with the actual username of the admin account. Type in the account password once prompted.

    su username

Step 2. Install the `homebrew` package manager as needed by following the instructions on https://brew.sh/. The purpose of this step is to allow installing SYMPHONY with it.

Step 3. Install SYMPHONY and its dependencies by running the following in terminal. Each line must be run separately.

    brew tap coin-or-tools/coinor

    brew install symphony

    brew install pkg-config

    brew cask install gfortran

Rsymphony solver package should now be able to locate the SYMPHONY libraries. Retry installing the Rsymphony solver package in R.

If the install fails, proceed to the next step.

### Modify the Rsymphony solver package to manually point to SYMPHONY libraries

Step 1. Install `wget` by running the following line in terminal. The purpose of this step is to allow downloading the package file directly from CRAN servers.

    brew install wget

Step 2. Run the following line in terminal to download the Rsymphony solver package.

    sudo wget https://cran.revolutionanalytics.com/src/contrib/Rsymphony_0.1-29.tar.gz

Step 3. Unpack the downloaded file by running the following line.

    sudo tar -xzvf Rsymphony_0.1-29.tar.gz

Step 4. Open the file `Rsymphony/configure` by running the following line. This opens `nano` text editor, with `-c` option to make it display the line numbers.

    sudo nano -c ./Rsymphony/configure

Comment out lines 51-55 by inserting `#`s at the beginning of each line, as shown below. Save it as the same file with Control + O, and when prompted for file name to write press Enter. Exit `nano` with Control + X.

    #if test ${status} -ne 0; then
    # echo "Cannot find SYMPHONY libraries and headers."
    # echo "See https://projects.coin-or.org/SYMPHONY."
    # exit 1
    #fi

Step 5. Open the file `Rsymphony/src/R_symphony.cc` by running the following line.

    sudo nano -c ./Rsymphony/src/R_symphony.cc

Change line 12 `#include <coin/symphony.h>` as shown below. Save it as the same file with Control + O, and when prompted for file name to write press Enter. Exit with Control + X.

    #include <symphony.h>

Step 6. Run the following line in terminal to install the modified Rsymphony package.

    sudo R CMD install Rsymphony

This will make the Rsymphony solver package available in R.

Do NOT run `install.packages('Rsymphony')` within R after Step 6. Doing so will trigger a re-install of the unmodified package.

If Step 6 fails with `--lgfortran` error, proceed to the next step.

### Tell R where Fortran libraries are located

The cause of the error is that R needs Fortran libraries to build the package, but it does not know where they are. They are already available on your system: when you installed SYMPHONY libraries in the previous step, it installed gcc library as a requirement, which contains Fortran libraries as well.

Step 1. Open the file `~/.R/Makevars` by running the following line.

    sudo nano -c ~/.R/Makevars

The file will be empty in most cases. Add the following line to point R to the Fortran libraries. Save it as the same file with Control + O, and when prompted for file name to write press Enter. Exit with Control + X.

    FLIBS = -L/usr/local/gfortran/lib -lgfortran -lquadmath -lm

Step 2. Run the following line in terminal to install the modified Rsymphony package.

    sudo R CMD install Rsymphony

This will make the Rsymphony solver package available in R.

Do NOT run `install.packages('Rsymphony')` within R after Step 2. Doing so will trigger a re-install of the unmodified package.

### Resources

For further reference, please refer to these following external links.

- https://stackoverflow.com/questions/32129191/osx-installing-rsymphony-linking-headers-and-libs/37599406
- https://stackoverflow.com/questions/42215523/installing-rsymphony-using-rstudio-on-macos