inst/app/README.md

OHI Shiny app

These files provide the template for creating a Shiny app that visualizes a data branch of an Ocean Health Index (OHI) repository.

Normally these files are translated from template files into a live Shiny app using the ohirepos::deploy_app() function, like so:

library(ohirepos)

# ohi-global
deploy_app('ohi-global', 'Global', c('eez2015','eez2012','eez2013','eez2014','eez2016'), projection='Mollweide')

# bhi
deploy_app('bhi', 'Baltic', 'baltic2015')

When the Shiny app first launches (eg with shiny::runApp()), the github repository specified in app.yml will be git cloned into the [gh_repo]_[gh_branch_data] folder from Github and all the scenarios (specified by scenario_dirs) will be loaded into [gh_repo]_[scenario].Rdata file(s) before launching the app.

Debugging

In practice, for developing this Shiny app, I launch RStudio with app.Rproj to set the working directory here, and create the app.yml and [repo]_intro.md for whichever repo before launching the app:

# set working directory
setwd('~/github/ohirepos/inst/app')

# vars for ohi-global
gh_repo        = 'ohi-global'
study_area     = 'Global'
scenario_dirs  = c('eez2016','eez2015')
projection     = 'Mollweide'

# vars for bhi
gh_repo        = 'bhi'
study_area     = 'Baltic'
scenario_dirs  = 'baltic2015'
projection     = 'Mercator'

# common vars
gh_owner       = 'OHI-Science'
gh_branch_data = 'draft'
gh_branch_app  = 'app'
map_shrink_pct = 10
debug          = F

# derived vars
app_url         = sprintf('http://ohi-science.nceas.ucsb.edu/%s', gh_repo)
ohirepos_commit = devtools:::local_sha('ohirepos')

readr::write_file(
    yaml::as.yaml(list(
    study_area      = study_area,
    gh_owner        = gh_owner,
    gh_repo         = gh_repo,
    gh_branch_data  = gh_branch_data,
    gh_branch_app   = gh_branch_app,
    app_url         = app_url,
    scenario_dirs   = scenario_dirs,
    projection      = projection,
    map_shrink_pct  = map_shrink_pct,
    debug           = F,
    ohirepos_commit = ohirepos_commit,
    gh_data_commit  = NULL,
    last_updated    = Sys.Date())),
    'app.yml')

# run app
shiny::runApp()

Notice that the existing .gitignore includes app.yml,intro.md, baltic2015.Rdata files and draft folder, so these will not be uploaded to the generic app template on Github. For working with various apps, I move them to a prefix (eg bhi_*) and add to .gitignore.

Credit

The majority of this Shiny application was coded by Ben Best with significant inputs from Julie Lowndes. Special thanks to Herman Sontrop who developed the ohi-aster htmlwidget and these Shiny JavaScript tutorials.

GeoJSON Simplification

The initial loading time and overall performance of the Shiny app in a web browser is very dependent on the size of the GeoJSON file used to render the regions spatially. The path of this geojson file is specified as the geojson variable in the config.R of the data branch's scenario subfolder (eg for Baltic draft branch, baltic2015 scenario: config.R -> spatial/regions_gcs.geojson:

geojson = 'spatial/regions_gcs.geojson'

Here's how the high resolution global EEZ shapefile was greatly simplified with the excellent rmapshaper::ms_simplify to ohi-global/rgn_offshore_gcs_mapshaper-simplify_x2_eez-only.geojson...

## install.packages("devtools")
library(dplyr)
library(sp)
library(rgdal)
library(geojsonio)
library(rmapshaper) # install_github("ateucher/rmapshaper")

# downloaded from sftp://neptune.nceas.ucsb.edu/var/data/ohi/git-annex/Global/NCEAS-Regions_v2014/data/rgn_offshore_gcs.shp
shp = '~/Documents/OHI/spatial/rgn_offshore_gcs.shp'

# read in polygon
ply = readOGR(normalizePath(dirname(shp)), basename(tools::file_path_sans_ext(shp)), verbose=F)

# subset
sub = ply[ply@data$rgn_name %in% c('Costa Rica','Nicaragua'),]

# plot
plot(sub, col='gray')

# simplify using default parameters
ply_simp0 = ms_simplify(ply)
plot(ply_simp0, col='gray')

# write out as geojson
geojson_write(ply_simp0, file='data/rgn_offshore_gcs_mapshaper-simplify.geojson')
})

# simplify from existing geojson, plot and write out
ply_simp0 = readOGR('data/rgn_offshore_gcs_mapshaper-simplify.geojson', 'OGRGeoJSON', verbose=F)
plot(ply_simp0, col='gray')
ply_simp1 = ms_simplify(ply_simp0)
plot(ply_simp1, col='gray')
geojson_write(ply_simp1, file='data/rgn_offshore_gcs_mapshaper-simplify_x2.geojson')

# remove non-eez shapes
ply_simp2 = ply_simp1[ply_simp1$rgn_type=='eez' & ply_simp1$rgn_name!='Antarctica',]
plot(ply_simp2, col='gray')
geojson_write(ply_simp2, file='data/rgn_offshore_gcs_mapshaper-simplify_x2_eez-only.geojson')


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