R code to calculate OHI scores with the ohicore package.

This is an RMarkdown file, where written text appears with a white background and R code appears with a grey background as a "code chunk". You can run R code line-by-line, or as a whole chunk by clicking the green triangle at the top-right corner of the code chunk's grey box.


Install R packages

Run this code chunk one time only to install packages you don't already have. This is like wiring a building for electricity. Note: warnings are fine.

## install packages from R community
install.packages("tidyverse")
install.packages("zoo")
install.packages("here")
install.packages("devtools")

## install the ohicore package from OHI team
devtools::install_github('ohi-science/ohicore')

Load R packages

Run this every time you calculate scores so that you will have libraries available to you. This is like turning on the lights in a building. Additionally, you will set the working directory to your scenario folder. The syntax :: identifies which library a function is from.

## load package libraries
library(tidyverse)
library(stringr)
library(zoo)
library(here)
library(ohicore)

## set the working directory to a filepath we all have
setwd(here::here('<%=scenario%>'))

Configure Toolbox

Run this chunk each time you calculate scores to ensure that all files in your repo are properly configured. These functions from ohicore (ohicore::) will check your repo's files to make sure you have registered layers properly, etc. It will create conf and layers objects that will be used to calculate scores. Note: you must re-run this code chunk before calculating scores if you have made changes to functions.R, goals.csv, etc.

## load scenario configuration
conf <- ohicore::Conf('conf')

## check that scenario layers files in the \layers folder match layers.csv registration. Layers files are not modified.
ohicore::CheckLayers('layers.csv', 'layers', flds_id=conf$config$layers_id_fields)

## load scenario layers for ohicore to access. Layers files are not modified.
layers <- ohicore::Layers('layers.csv', 'layers')

## select scenario year for the assessment
scenario_years <- <%=scenario_year%>
layers$data$scenario_year <- scenario_years

Calculate and save scores

Run this chunk to calculate and save scores as scores object and as scores.csv. You can examine scores.csv in the Git tab of RStudio to explore what changed.

## calculate scenario scores
scores <- ohicore::CalculateAll(conf, layers)

## save scores as scores.csv
readr::write_csv(scores, 'scores.csv', na='')

Create figures

Run this to create flower plots for each region.

## source script (to be incorporated into ohicore)
source('https://raw.githubusercontent.com/OHI-Science/arc/master/circle2016/plot_flower_local.R')

PlotFlower(assessment_name = "Toolbox Demo",
dir_fig_save    = "reports/figures")

Example workflow

These code chunks will help you as you develop individual goal models in conf/functions.R. A good workflow is:

  1. prepare data layers in the prep/ folders (script as much as possible in R)
  2. register data layers in layers.csv and save them in layers/ folder
  3. update information in conf/scenario_data_years.csv
  4. run the Configure Toolbox code chunk
  5. develop goal models in functions.r, running individual goal models line by line
  6. calculate scores when the model is developed (remember to re-run Configure Toolbox first!)


OHI-Science/ohirepos documentation built on June 1, 2024, 12:21 p.m.