---
title: "Installing Rsymphony solver package on Mac"
output: 
  html_document: 
    highlight: null
vignette: >
  %\VignetteIndexEntry{Installing Rsymphony solver package on Mac}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{knitr::rmarkdown}
---

<br/>

### Introduction

Rsymphony is one of the solver packages supported by TestDesign. As of Rsymphony version 0.1.28, Mac users are likely to encounter error messages like below when the user tries to install the solver package from R. Alternatively, 'lpsymphony' from Bioconductor repository is supported from TestDesign 1.0.0.

```
* 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.

<br/>
<br/>

### Install SYMPHONY libraries

The cause of the error is that Rsymphony requires SYMPHONY libraries, but the libraries are not 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 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 by following the instructions on http://brew.sh/. The purpose of this step is to allow installing SYMPHONY with it.

**Step 3.** Install SYMPHONY 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.

<br/>
<br/>

### 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-28.tar.gz 
```

**Step 3.** Unpack the downloaded file with running the following line.

```
sudo tar -xzvf Rsymphony_0.1-28.tar.gz
```

**Step 4.** Open the file `Rsymphony/configure` with 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 the lines 51-55 by inserting `#`s at the beginning of each line as below. Save it as the same file with Control + O, and when prompted for file name to write press Enter. Exit 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 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.

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

<br/>
<br/>

### 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 the SYMPHONY libraries from the previous steps, 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 on 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.

<br/>
<br/>

### 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

