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.
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')
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%>'))
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
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='')
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")
These code chunks will help you as you develop individual goal models in conf/functions.R
. A good workflow is:
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.