2 Setting up your computer
You will need to have both R and RStudio installed on your computer to complete this workshop. Although it is not imperative that you have the latest version of RStudio installed, you will need to have at least version 4.0 of R installed . Please note that you might need administrative permissions to install these programs. After installing them, you will also need to install some R packages too. Finally, you will also need to download the data for this workshop.
2.1 R
The R statistical computing environment can be downloaded from the Comprehensive R Archive Network (CRAN). Specifically, you can download the latest version of R (version 4.0.3) from here: https://cloud.r-project.org. Please note that you will need to download the correct file for your operating system (i.e. Linux, Mac OSX, Windows).
2.2 RStudio
RStudio is an integrated development environment (IDE). In other words, it is a program that is designed to make your R programming experience more enjoyable. During this workshop, you will interact with R through RStudio—meaning that you will open RStudio to code in R. You can download the latest version of RStudio here: http://www.rstudio.com/download. When you start RStudio, you will see two main parts of the interface:

You can type R code into the Console and press the enter key to run code.
2.3 R packages
An R package is a collection of R code and documentation that can be installed to enhance the standard R environment with additional functionality. Currently, there are over fifteen thousand R packages available on CRAN. Each of these R packages are developed to perform a specific task, such as reading Excel spreadsheets, downloading satellite imagery data, downloading and cleaning protected area data, or fitting environmental niche models. In fact, R has such a diverse ecosystem of R packages, that the question is almost always not “can I use R to …?” but “what R package can I use to …?”. During this workshop, we will use several R packages. To install these R packages, please enter the code below in the Console part of the RStudio interface and press enter. Note that you will require an Internet connection and the installation process may take some time to complete.
install.packages(c("sf", "dplyr", "sp", "rgeos", "rgdal", "raster",
"units", "prioritizr", "prioritizrdata",
"mapview", "assertthat", "remotes", "gridExtra",
"BiocManager"))
BiocManager::install("lpsymphony")
Some of the new code in this workshop relies on prioritizr R package v6.0 which is only available on Github at the moment. The CRAN version is currently 5.03. You do not have to install v6.0 if you don’t wish to. There are only a few instances of code which will not be able to run. But if you want to try v6.0 you can run:
devtools::install_github("prioritizr/prioritizr")
This will require you to have all the tools required for building packages installed on your computer but RStudio should help with the process and you can get help from here: https://support.rstudio.com/hc/en-us/articles/200486498-Package-Development-Prerequisites.
2.4 Optimisation Software
While it possible to use the lpsymphony
package installed above, Gurobi is the most powerful and fastest solver that the prioritizr R package can use to solve conservation planning problems. This section will walk you through the process of setting up Gurobi on your computer. If you encounter any problems while following the instructions below, check out the official Gurobi documentation. If you can’t solve the problems, don’t worry. We can use lpsymphony
.
2.4.1 Obtaining a license
Gurobi is a commercial computer program. This means that users will need to obtain a license for Gurobi before they can use it. Although academics can obtain a special license at no cost, individuals that are not affiliated with a recognized educational institution may need to purchase a license to use Gurobi. If you are an academic that is affiliated with an educational institution, you can take advantage of the special academic license to use Gurobi for no cost. Once you have signed up for a free account you can request a free academic license.

Once you accept the Terms Of Service you can generate a license.

Now, copy and save the grbgetkey XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
command for later use.
2.4.2 Downloading the Gurobi software suite
After obtaining a license, you will need to download a copy of the Gurobi installer to your computer. To achieve this, visit the Gurobi downloads web page and download the correct version of the installer for your operating system.
2.4.3 Software installation
The installation process for the Gurobi software suite depends on the type of operating system you have installed on your computer. Fortunately, Gurobi provide platform-specific “Quick Start Guides” for Windows, Mac OSX, and Linux systems that should help with this. Briefly, on Windows and Mac systems, you just need to double-click on the Gurobi installer, follow the prompts, and the installer will take care of rest.
After installing the Gurobi software suite on your computer, you will need to activate your license.
2.4.4 License activation
Now we will activate the Gurobi software using the license you downloaded earlier. Please note that the correct set of instructions depends on your system and license. In most cases, you should follow the instructions in the “Local license activation”. If, and only if, you are activating a special Academic license on a networked computer that is not connected to your university’s network (e.g. a cloud-based system), then please follow the instructions below in the “Cloud license activation over SSH” section.
2.4.5 Local license activation
To activate the license, simply copy and paste the grbgetkey
command into your computer’s command prompt or terminal (note that Windows users can open the command prompt by typing cmd
in the search box and pressing the enter
key). After running the grbgetkey
command with the correct license code, you should see output that looks something like that in the screen shot below.

2.4.6 Install gurobi R
To install the R package, instructions can be found here. Briefly you can install it by substituting <R-package-file>
in the code below for the location of the Gurobi software on your computer.
install.packages('<R-package-file>', repos=NULL)
# For a Mac it would be:
install.packages("/Library/gurobi911/mac64/R/gurobi_9.1-1_R_4.0.2.tgz", repos = NULL)
Depending on your local R environment you might need to install the R package slam. To do this, you should issue the following command within R:
install.packages('slam')
2.5 Test your install
You can check if you have successfully installed Gurobi by running the code below. If you don’t get any errors, you are good to go. Don’t worry about what it means. That is what we are going to learn.
library(prioritizr)
data(sim_pu_raster)
data(sim_features)
p <- problem(sim_pu_raster, sim_features) %>%
add_min_set_objective() %>%
add_binary_decisions() %>%
add_relative_targets(0.1) %>%
add_gurobi_solver(verbose = FALSE)
s <- solve(p)
If you couldn’t install Gurobi, and instead went with lpsymphony, try substituting add_gurobi_solver(verbose = FALSE)
with add_lpsymphony_solver(verbose = FALSE)
.