# Chapter 10 Calculations: advanced workflow

In development.

The purpose of Chapter 10 is to build off the basic workflow for calculating OHI scores and do a few more advanced things. This is a a 2-hour hands-on training: you will be following along on your own computer and working with a copy of the demonstration repository that is used throughout the this chapter.

## 10.1 Overview

This chapter will show you how to add and remove goals from your assessment. You will be able to use these same principles to turn a subgoal into a goal, or rename goals altogether. The toolbox will calculate goal scores for each goal listed in conf/goals.csv, so investigating and modifying this file will be a big focus of this chapter.

As in Chapter 10, it is good practice to rerun calculate_scores.Rmd after modifying these files and and use Git tab in RStudio to check your work.

### 10.1.1 Prerequisites

Before the training, please make sure you have done the following:

1. Have up-to-date versions of R and RStudio and have RStudio configured with Git/GitHub
2. Fork the toolbox-demo repository into your own GitHub account by going to https://github.com/OHI-Science/toolbox-demo, clicking “Fork” in the upper right corner, and selecting your account
3. Clone the toolbox-demo repo from your GitHub account into RStudio into a folder called “github” in your home directory (filepath “~/github”)
4. Get comfortable: be set up with two screens if possible. You will be following along in RStudio on your own computer while also watching an instructor’s screen or following this tutorial.

Additionally, this chapter assumes you are familiar with content from Chapter 8, so review that first if needed.

## 10.2 Explore goals.csv

goals.csv is the registry for the goals that will be included in OHI calculations. ohicore will use this list and make sure there are functions with the same names in functions.R. This is one of the things that the Toolbox does with the Conf() command, as we will see.

goals.csv is inside the conf folder. It is a .csv file has a lot of columns, so it is easier to open and inspect it in Excel or other spreadsheet software. Let’s discuss a few of the columns here (see Chapter 6 for descriptions of all of them):

goals.csv has information for how goal scores are calculated, and how information will be visualized and communicated. Let’s focus on this now:

• Each goal or subgoal has its own row. the “goal” column lists them all
• If the “goal” column is really a subgoal, then the “parent” column lists the parent or supragoal of that sub-goal

## 10.3 Round 1: Poke at it to see how it works

The Toolbox won’t always give errors when you start changing things (although we are improving that), so let’s just see what some of these things look like. This is when using the Git tab to help you keep track of how things work REALLY helps.

We will make modifications in goals.csv and see how that affects the basic calculations workflow from Chapter 10, and then we will do the same with functions.R.

### 10.3.1 Remove a goal (whole row) in goals.csv

For example, delete the entire “LSP” row in the .csv file, and save it. You should see the goals.csv show up in your Git tab in RStudio because the file has been modified.

Now, recalculate scores from calculate_scores.Rmd (remember to run Configure Toolbox first to rerun the Conf() function!)

What changed?

As we see from the Git tab, removing this goal affected scores for three things: “Index” and “SP” scores changed, and “LSP” scores were completely deleted.

## 10.4 Round 2: Break it to see how it works

### 10.4.1 Change preindex_function goals.csv

it’s the preindex_function column that is critical for the functions. Let’s add a typo there.

# Calculating Status and Trend for each region for HAB...
# Error in eval(expr, envir, enclos) : could not find function "HA"

### 10.4.2 Add a new goal in goals.csv

Just copy-paste.

What do you think will happen? what’s the most important column? That’s right, preindex_function.

This will fail at the scores calculation.

scores = ohicore::CalculateAll(conf, layers)
# Running Setup()...
# Calculating Status and Trend for each region for FIS...
# ...
# ...
# Calculating Status and Trend for each region for SPP...
# Calculating Status and Trend for each region for EX...
# Error in eval(expr, envir, enclos) : could not find function "EX"
write.csv(scores, 'scores.csv', na='', row.names=F)
# Error in is.data.frame(x) : object 'scores' not found

### 10.4.3 Delete a goal model from functions.r

won’t fail at Conf, but will at scores calc.

# Calculating Status and Trend for each region for AO...
# Error in eval(expr, envir, enclos) : could not find function "AO"
# ...
write.csv(scores, 'scores.csv', na='', row.names=F)
# Error in is.data.frame(x) : object 'scores' not found