-
Notifications
You must be signed in to change notification settings - Fork 3
/
CFRG_Review.Rmd
365 lines (332 loc) · 15.3 KB
/
CFRG_Review.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
---
title: "CFRG Review"
author: "Kiri Daust"
date: "07/10/2021"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, eval = TRUE, include = TRUE, message = FALSE, warning = FALSE)
library(shiny)
library(ccissr)
library(data.table)
library(pool)
library(RPostgreSQL)
pool <- dbPool(
drv = RPostgres::Postgres(),
dbname = Sys.getenv("BCGOV_DB"),
host = Sys.getenv("BCGOV_HOST"),
port = 5432,
user = Sys.getenv("BCGOV_USR"),
password = Sys.getenv("BCGOV_PWD")
)
sppDb <- dbPool(
drv = RPostgres::Postgres(),
dbname = "spp_feas",
host = Sys.getenv("BCGOV_HOST"),
port = 5432,
user = Sys.getenv("BCGOV_USR"),
password = Sys.getenv("BCGOV_PWD")
)
S1 <- setDT(dbGetQuery(sppDb,"select bgc,ss_nospace,spp,newfeas from feasorig"))
setnames(S1,c("BGC","SS_NoSpace","Spp","Feasible"))
gcm_weight <- data.table(gcm = c("ACCESS-ESM1-5", "BCC-CSM2-MR", "CanESM5", "CNRM-ESM2-1", "EC-Earth3",
"GFDL-ESM4", "GISS-E2-1-G", "INM-CM5-0", "IPSL-CM6A-LR", "MIROC6",
"MPI-ESM1-2-HR", "MRI-ESM2-0", "UKESM1-0-LL"),
weight = c(1,1,0,1,1,1,1,0,1,1,1,1,0))
rcp_weight <- data.table(rcp = c("ssp126","ssp245","ssp370","ssp585"),
weight = c(0.8,1,0.8,0))
all_weight <- as.data.table(expand.grid(gcm = gcm_weight$gcm,rcp = rcp_weight$rcp))
all_weight[gcm_weight,wgcm := i.weight, on = "gcm"]
all_weight[rcp_weight,wrcp := i.weight, on = "rcp"]
all_weight[,weight := wgcm*wrcp]
# Function to format Species with footnotes
sppnotes <- function(spp, notes, textstyle) {
ret <- vector("list", length(spp))
for (i in seq_len(length(spp))) {
fn <- paste0(sort(as.integer(unique(unlist(notes[i])))), collapse = ",")
ret[[i]] <- tags$span(tags$span(style = textstyle[i], spp[i]),
tags$sup(fn, .noWS = htmltools:::noWSOptions),
if (i < length(spp)) {", "} else {""}, .noWS = htmltools:::noWSOptions)
}
ret[["style"]] <- "white-space:normal;"
do.call(tags$td, ret)
}
sppnotes_cciss <- function(spp, textstyle) {
ret <- vector("list", length(spp))
for (i in seq_len(length(spp))) {
ret[[i]] <- tags$span(tags$span(style = textstyle[i], spp[i]),
if (i < length(spp)) {", "} else {""}, .noWS = htmltools:::noWSOptions)
}
ret[["style"]] <- "white-space:normal; border-left: 2px solid;"
do.call(tags$td, ret)
}
# Function to create a Standards block for each Standard in the site serie
##silv functions
standardblocks <- function(data, siteref, siteserie) {
sc <- data[
SiteRef %in% siteref & SS_NoSpace %in% siteserie,
list(Region, ZoneSubzone, SS_NoSpace, ccissFeas, Improve,PrefAcc,EstabFeas, Spp)
]
ss <- stocking_standards[
Region %in% sc$Region & ZoneSubzone %in% sc$ZoneSubzone & SS_NoSpace %in% sc$SS_NoSpace
]
do.call(span, lapply(unique(ss$Standard), standardblock, ss = ss, sc = sc))
}
standardblock <- function(std, ss, sc) {
ss <- ss[Standard %in% std]
ss[, TextStyle := ""]
sc[,TxtCciss := ""]
# Some logic to flag specie with different Suitability than CCISS
ss[sc, on = "Species==Spp", ProjFeas := suppressWarnings(as.integer(i.ccissFeas))]
setnafill(ss, fill = 4L, cols = "ProjFeas")
ss[Suitability > ProjFeas, TextStyle := "color:green"]
ss[Suitability < ProjFeas, TextStyle := "color:red"]
ss[!ProjFeas %in% c(1,2,3), TextStyle := "color:red;text-decoration:line-through"]
ss[Suitability == 0, TextStyle := NA]
#browser()
# cciss colouring
sc[ss, on = "Spp == Species", CFRGSuit := i.Suitability]
sc[ccissFeas < CFRGSuit, TxtCciss := "color:green"]
sc[ccissFeas > CFRGSuit, TxtCciss := "color:red"]
sc[!CFRGSuit %in% c(1,2,3), TxtCciss := "color:purple"]
sc[CFRGSuit == 0, TxtCciss := NA]
si <- stocking_info[Standard == std]
sh <- stocking_height[Standard == std]
list(
tags$small("Forest Region: ", tags$b(si$Region, .noWS = c("before", "after")), .noWS = "inside"),
tags$small("\nStandards ID: ",tags$b(paste(ss[!is.na(Standard), unique(Standard)], collapse = ", "),.noWS = c("before", "after"))),
tags$table(style = "max-width: 100%; white-space: nowrap;",
# Report formatting gray out the first row, so faking a row
tags$tr(
tags$td(width = "50%", style = "vertical-align: top; padding:0; background-color:white; border:none",
tags$small(tags$b("Regeneration")),
tags$hr(style = "padding: 0; margin: 0 0 3px 0; height: 2px; background-color: darkgreen; border: 0px"),
tags$table(
width = "100%",
tags$tr(
tags$td(tags$b("Feasibility")),
tags$td(tags$b("CFRG"),style = "border-right: 2px solid;"),
tags$td(tags$b("CCISS"))
),
tags$tr(
tags$td("Primary/E1"),
ss[!is.na(Species) & Suitability %in% 1L, sppnotes(Species, Footnotes, TextStyle)],
sc[!is.na(Spp) & ccissFeas %in% "1", sppnotes_cciss(Spp,TxtCciss)]
),
tags$tr(
tags$td("Secondary/E2"),
ss[!is.na(Species) & Suitability %in% 2L, sppnotes(Species, Footnotes, TextStyle)],
sc[!is.na(Spp) & ccissFeas %in% "2", sppnotes_cciss(Spp,TxtCciss)]
),
tags$tr(
tags$td("Tertiary/E3"),
ss[!is.na(Species) & Suitability %in% 3L, sppnotes(Species, Footnotes, TextStyle)],
sc[!is.na(Spp) & ccissFeas %in% "3", sppnotes_cciss(Spp,TxtCciss)]
),
tags$tr(
tags$td("Trial"),
tags$td(""),
sc[!is.na(Spp) & EstabFeas == "Trial", sppnotes_cciss(Spp,TxtCciss)]
),
tags$tr(
tags$td("Broadleaf"),
ss[!is.na(Species) & Suitability %in% 0L, sppnotes(Species, Footnotes, TextStyle)],
tags$td("",style = "border-left: 2px solid;"),
style = "border-bottom:1px solid black;"
),
tags$tr(
tags$td("Preferred (p)"),
ss[!is.na(Species) & PreferredAcceptable %in% "P", sppnotes(Species, Footnotes, TextStyle)],
sc[!is.na(Spp) & PrefAcc %in% "P", sppnotes_cciss(Spp,TxtCciss)],
),
tags$tr(
tags$td("Acceptable (a)"),
ss[!is.na(Species) & PreferredAcceptable %in% "A", sppnotes(Species, Footnotes, TextStyle)],
sc[!is.na(Spp) & PrefAcc %in% "A", sppnotes_cciss(Spp,TxtCciss)],
)
)
),
tags$td(width = "50%", style = "vertical-align: top; padding:0px 0px 0px 8px; background-color:white; border:none",
tags$small(tags$b("Stocking (i) - well spaced/ha")),
tags$hr(style = "padding: 0; margin: 0 0 3px 0; height: 2px; background-color: darkgreen; border: 0px"),
tags$table(
width = "100%",
tags$tr(
tags$td(tags$b("Target")),
tags$td(tags$b("Min pa")),
tags$td(tags$b("Min p")),
tags$td(tags$b("Regen Delay (max yrs)"))
),
tags$tr(
tags$td(si$StockingTarget),
tags$td(si$StockingMINpa),
tags$td(si$StockingMINp),
tags$td(si$StockingDelay)
)
),
tags$br(),
tags$small(tags$b("Free Growing Guide")),
tags$hr(style = "padding: 0; margin: 0 0 3px 0; height: 2px; background-color: #003366; border: 0px"),
tags$table(
width = "100%",
tags$tr(
tags$td(tags$b("Earliest (yrs)")),
tags$td(tags$b("Latest(yrs)")),
tags$td(tags$b("Min Height (m)")),
tags$td(tags$b("Min Height (m)"))
),
tags$tr(
tags$td(si$AssessmentEarliest),
tags$td(si$AssessmentLatest),
tags$td(style = "white-space: normal;", sh[!Flag %in% TRUE, paste(Species, Height, sep = ": ", collapse = ", ")]),
tags$td(style = "white-space: normal;", sh[Flag %in% TRUE, paste(Species, Height, sep = ": ", collapse = ", ")])
)
)
)
),
tags$tr(
tags$td(colspan = "2", style = "white-space:normal; vertical-align: top; padding:0; background-color:white; border:none",
tags$small(tags$b("Footnotes")),
tags$hr(style = "padding: 0; margin: 0 0 3px 0; height: 2px; background-color: #003366; border: 0px"),
{
fn <- ss[PreferredAcceptable %in% c("A", "P") | Suitability %in% 1:3, sort(as.integer(unique(unlist(Footnotes))))]
fnt <- footnotes[match(fn, `Revised Footnote`), `Revised Footnote Text`]
fnshiny <- mapply(function(footnote, text) {list(tags$sup(footnote), tags$small(text), tags$br())}, fn, fnt, SIMPLIFY = FALSE, USE.NAMES = FALSE)
do.call(span, fnshiny)
}
)
)
)
)
}
reg_cw <- fread("District_Region_Crosswalk.csv")
```
```{r, results='asis'}
dist_select <- "DMH"
bgcFlags <- list()
bgc_opts <- dbGetQuery(pool,paste0("select distinct bgc from preselected_points where dist_code = '",dist_select,"'"))[,1]
for(bgc_select in bgc_opts){
pNums <- dbGetBGC(pool,bgc_select,dist_select,100)
if(length(pNums) > 25){
dat <- dbGetCCISS(pool,pNums,T,all_weight)
eda <- edatopicOverlap(dat,E1,E1_Phase,onlyRegular = T)
feas <- ccissOutput(eda,S1,R1,F1,c(0.3,0.35,0.35),c(0.3,0.3,0.3,0.1))
feasSum <- feas$Summary
setnames(feasSum,old = "ccissSuit", new = "ccissFeas")
feasSum[,Region := reg_cw[dist_code == dist_select,reg_cfrg]]
feasSum[,ZoneSubzone := SiteRef]
feasSum[,EstabFeas := NewSuit]
feasSum[
stocking_standards,
`:=`(CFSuitability = as.character(i.Suitability)),
on = c(Region = "Region", ZoneSubzone = "ZoneSubzone", SS_NoSpace = "SS_NoSpace", Spp = "Species")
]
siteseries <- unique(feasSum$SS_NoSpace)
flagVals <- data.table(ss_nospace = siteseries, flag = 0)
for(siteserie in siteseries){
f1 <- feasSum[SS_NoSpace == siteserie,]
flagDat <- f1[,.(Spp, ccissFeas, CFSuitability)]
flagDat <- flagDat[CFSuitability %in% c(1,2,3),]
flagDat[ccissFeas == "X", ccissFeas := 5]
flagDat[,ccissFeas := as.numeric(ccissFeas)]
flagDat[,Diff := ccissFeas - as.numeric(CFSuitability)]
flagDat[Diff < 0, Diff := 0]
flagVals[ss_nospace == siteserie, flag := sum(flagDat$Diff)]
}
bgcFlags[[bgc_select]] <- flagVals
}
}
```
```{r index, results='asis',tidy=FALSE,eval=TRUE}
tags$h2("All BGC Units")
for(i in 1:length(bgc_opts)){
bgc_select <- bgc_opts[i]
temp <- dbGetQuery(pool,paste0("select count(*) from preselected_points where bgc = '",bgc_select,"' and dist_code = '",dist_select,"'"))[,1]
if(temp[1] > 25){
cat("\n[Site Unit ",bgc_select,"](#section",i,") \n",sep = "")
#cat(sprintf("<a href=\"#Site_Unit:%s\">- Unit %s</a><br>", bgc_select,bgc_select))
}
}
```
```{r,results='asis', out.height="auto"}
bgcNum = 1;
for(bgc_select in bgc_opts){
cat("<P style='page-break-before: always'>") #forced new-page happens here.
pNums <- dbGetBGC(pool,bgc_select,dist_select,100)
if(length(pNums) > 25){
dat <- dbGetCCISS(pool,pNums,T,all_weight)
eda <- edatopicOverlap(dat,E1,E1_Phase,onlyRegular = T)
feas <- ccissOutput(eda,S1,R1,F1,c(0.3,0.35,0.35),c(0.3,0.3,0.3,0.1))
feasSum <- feas$Summary
setnames(feasSum,old = "ccissSuit", new = "ccissFeas")
feasSum[,Region := reg_cw[dist_code == dist_select,reg_cfrg]]
feasSum[,ZoneSubzone := SiteRef]
feasSum[,EstabFeas := NewSuit]
feasSum[
stocking_standards,
`:=`(CFSuitability = as.character(i.Suitability)),
on = c(Region = "Region", ZoneSubzone = "ZoneSubzone", SS_NoSpace = "SS_NoSpace", Spp = "Species")
]
feasSum[cfrg_rules,PrefAcc := i.PrefAcc, on = c("Spp",ccissFeas = "Feasible")]
feasSum[is.na(PrefAcc),PrefAcc := "X"]
feasSum[,NoPref := if(any(PrefAcc == "P")) T else F, by = .(SS_NoSpace)]
feasSum[NoPref == F & PrefAcc == "A", PrefAcc := "P"]
feasSum[,NoPref := NULL]
feasSum[CFSuitability == "X" & Curr == "X"
& ccissFeas %in% c(1,2,3), EstabFeas := "Trial"]
siteseries <- unique(feasSum$SS_NoSpace)
cat("\n")
cat("## BGC: ",bgc_select," {#section",bgcNum,"}\n",sep = "")
for(i in 1:length(siteseries)){
ss_select <- siteseries[i]
flagSub <- bgcFlags[[bgc_select]]
if(flagSub[ss_nospace == ss_select,flag] > 6){
flagCol = "red"
}else if(flagSub[ss_nospace == ss_select,flag] > 4){
flagCol = "orange"
}else{
flagCol = "black"
}
cat("\n[<span style=\"color: ",flagCol,";\">Site Series ",ss_select,"</span>](#sssect",i,"-",bgcNum,") \n",sep = "")
}
ssnum = 1
for(siteserie in siteseries){
s1 <- stocking_standards[
Region %in% feasSum$Region & ZoneSubzone %in% feasSum$ZoneSubzone & SS_NoSpace %in% feasSum$SS_NoSpace
]
if(nrow(s1) > 0){
cat("\n")
cat("### Site Series: ",siteserie," {#sssect",ssnum,"-",bgcNum,"}\n",sep = "")
print(htmltools::tagList(
standardblocks(feasSum, bgc_select, siteserie),
))
}
ssnum = ssnum+1
}
cat("\n")
}
bgcNum = bgcNum+1
}
poolClose(pool)
```
```{=html}
<style type="text/css">
h2 {font-weight:900 !important;}
.main-content {max-width: none !important; padding:3rem 3rem !important}
.table {width: auto !important;}
.table td {vertical-align: top !important;}
table.table-centered {margin-left:auto !important; margin-right:auto !important}
</style>
```
```{=html}
<!-- <script type="text/javascript"> -->
<!-- // When the document is fully rendered... -->
<!-- $(document).ready(function() { -->
<!-- // ...select all header elements... -->
<!-- $('h1, h2, h3, h4, h5').each(function() { -->
<!-- console.log($(this).html()); -->
<!-- $(this).attr('id', $(this).html()); -->
<!-- }); -->
<!-- }); -->
<!-- </script> -->
```