Skip to content

Commit 5a8c926

Browse files
authored
Merge pull request #170 from inrae/page1_button
Page1 pop up with good informaton #153 Page1 remove division #152 Page1 button #168 Page 4 simulation #151 #167
2 parents 4f295bd + 7b6f392 commit 5a8c926

10 files changed

+22618
-23293
lines changed

R/mod_a_first.R

+22-13
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,6 @@ mod_first_ui <- function(id) {
2525
mod_species_ui(ns("species_ui_1"))
2626
)
2727
),
28-
actionButton(ns("showaqua"),
29-
label = 'AquaMaps'),
30-
actionButton(ns("positive_catch"),
31-
label = 'Positive catch'),
32-
33-
# radioButtons(
34-
# ns("showaqua"),
35-
# label = NULL,
36-
# choices = c(
37-
# "Hide AquaMaps" = "hide",
38-
# "Show AquaMaps" = "show"
39-
# )
40-
# ),
4128
w3css::w3_quarter()
4229
)
4330
),
@@ -60,6 +47,28 @@ mod_first_ui <- function(id) {
6047
),
6148
w3css::w3_col(
6249
class = "s2",
50+
actionButton(ns("showaqua"),
51+
label = 'AquaMaps',
52+
style = "background-color: #FFFF0080"),
53+
# radioButtons(
54+
# ns("showaqua"),
55+
# label = NULL,
56+
# choices = c(
57+
# "Hide AquaMaps" = "hide",
58+
# "Show AquaMaps" = "show"
59+
# )
60+
# ),
61+
w3_help_button(
62+
"Display AquaMpas",
63+
"display_aquamaps_help"
64+
),
65+
actionButton(ns("positive_catch"),
66+
label = with_i18('Positive catch', 'positive_catch_button'),
67+
style = "background-color: #00FF0080"),
68+
w3_help_button(
69+
"Display positive catch",
70+
"display_positive_catch_help"
71+
),
6372
h4(
6473
with_i18(
6574
"Conservation status",

R/mod_a_first_fct_map.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ tm_ocean <- function(dataOcean,
3838
title = paste0(title, "\n(", yearStart, "-", yearEnd, ")"),
3939
palette = c("#F7FBFF", "#C6DBEF", "#9ECAE1", "#4292C6", "#08519C", "#08306B"),
4040
n = 6,
41+
alpha = .5,
4142
border.col = "gray90",
4243
labels = c(
4344
"Not recorded in the period" %>% with_i18("absent") %>% as.character(),
@@ -144,7 +145,6 @@ bbox <- sf::st_bbox(c(xmin = -17.5, xmax = 19, ymax = 36, ymin = 62), crs = sf::
144145
#' @param spatial_type Geom to use in the map
145146
#' @param con The Connection object
146147
#' @param yearStart,yearEnd date used
147-
#' @param dataCatchment,catchment_geom,dataALL,ices_geom,ices_division internal datasets
148148
#' @param dataCatchment,catchment_geom internal datasets for continental waters
149149
#' @param dataALL,ices_geom,ices_division,positive_catch_area internal datasets for marines water
150150
#' @param session The Shiny Session object

data-raw/altas_simulation.R

+7-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# ==== Run this to create the dput for unit tests ====
2-
library(tictoc)
2+
#library(tictoc)
33
library(purrr)
44
# library(Rfast)
55
library(Matrix)
@@ -17,7 +17,6 @@ source('data-raw/preparation_atlas_simulation.R')
1717
hydiad_parameter %>%
1818
print()
1919

20-
2120
# Anthropogenic mortality ----
2221
# build from sliders in interface
2322
# here fake data
@@ -55,8 +54,7 @@ selected_latin_name = "Alosa alosa"
5554

5655
runSimulation_pml = function(selected_latin_name, hydiad_parameter, anthropogenic_mortality,
5756
catchment_surface, data_hsi_nmax, data_ni0, outlet_distance, verbose = FALSE) {
58-
if (verbose) tic()
59-
57+
6058
# --------------------------------------------------------------------------------------- #
6159
results = list()
6260

@@ -141,9 +139,6 @@ runSimulation_pml = function(selected_latin_name, hydiad_parameter, anthropogeni
141139
arrange(year)
142140
results[['param']][['years']] <- years
143141

144-
if (verbose) toc()
145-
146-
if (verbose) tic()
147142
# ------------------------------------------------------------------------------- #
148143
## compute Nmax_eh1 matrix and prepare Nit matrix ----
149144
resultsPM <- results[["model"]] <- lapply(models, function(model) {
@@ -255,8 +250,6 @@ runSimulation_pml = function(selected_latin_name, hydiad_parameter, anthropogeni
255250

256251
#Rq: transpose of Besty's matrix (not sure now)
257252

258-
if (verbose) toc()
259-
260253
# for testing: resultsModel <- results[['model']][[1]]
261254
# compute effective for 1 model ----
262255
computeEffectiveForModel_PML = function(model, currentYear, results, generationtime, nbCohorts){
@@ -333,7 +326,6 @@ runSimulation_pml = function(selected_latin_name, hydiad_parameter, anthropogeni
333326

334327

335328
# run simulation over years
336-
if (verbose) tic()
337329
for (currentYear in yearsToRun) {
338330
# currentYear <- yearsToRun[1]
339331
## print a progress bar to the console
@@ -343,17 +335,17 @@ runSimulation_pml = function(selected_latin_name, hydiad_parameter, anthropogeni
343335

344336
# dput(results, file = "tests/testthat/results_pml_dput")
345337
cat('\n')
346-
if (verbose) toc()
338+
347339

348340
return(results)
349341
}
350342

351343
# =======================================================================================================
352344
# run simulation ----
353-
tic()
345+
354346
results <- runSimulation_pml(selected_latin_name, hydiad_parameter, anthropogenic_mortality,
355347
catchment_surface, data_hsi_nmax, data_ni0, outlet_distance, verbose = FALSE)
356-
toc()
348+
357349

358350
dput(results, file = "tests/testthat/results_pml_dput")
359351
utils::zip("tests/testthat/results_pml_dput", zipfile = "tests/testthat/results_pml_dput.zip")
@@ -448,7 +440,8 @@ dput(model_res_filtered_pml, file = "tests/testthat/model_res_filtered_dput")
448440
model_res_filtered_pml %>%
449441
ggplot(aes(x = year)) +
450442
geom_ribbon(aes(ymin = min, ymax = max, fill = source), alpha = .5) +
451-
geom_line(aes(y = rolling_mean, colour = source, linetype = source),
443+
geom_line(data = . %>% filter(!is.na(rolling_mean)),
444+
aes(y = rolling_mean, colour = source, linetype = source),
452445
alpha = 0.9) +
453446
ylab('Nit')
454447

data-raw/preparation_atlas_simulation.R

+119-84
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,35 @@
11
library(DBI)
2+
# library(tidyverse)
23

3-
library(tictoc)
4-
library(tidyverse)
5-
6-
# rm(list = ls())
7-
connection_sql = TRUE
8-
9-
10-
# connection to the data base
11-
if (connection_sql)
12-
# conn_eurodiad <- dbConnect(RPostgres::Postgres(), dbname = 'eurodiad',
13-
# host = 'citerne.bordeaux.irstea.priv',
14-
# port = 5432,
15-
# user = 'patrick.lambert',
16-
# password = rstudioapi::askForPassword("Database password"))
17-
conn_eurodiad <- connect()
4+
pkgload::load_all(here::here()) # simulate installation and give access to objects in the package
5+
# session <- shiny::MockShinySession$new() #new.env()
6+
# connect(session)
7+
# con <- get_con(session)
8+
# connection to the data base
9+
conn_eurodiad <- connect()
1810

1911
# data upload ----
2012

2113
# ---------------------------------------------------------------------- #
2214
## Catchment features ----
23-
if (connection_sql) {
24-
data_catchment <- dbGetQuery(conn_eurodiad, "SELECT basin_id, basin_name, country, surface_area_drainage_basin as surface_area, ccm_area FROM diadesatlas.basin b
25-
INNER JOIN diadesatlas.basin_outlet bo USING (basin_id);" ) %>%
15+
data_catchment <- dbGetQuery(conn_eurodiad,
16+
"SELECT
17+
basin_id,
18+
basin_name,
19+
country,
20+
surface_area_drainage_basin as surface_area,
21+
ccm_area
22+
FROM
23+
diadesatlas.basin b
24+
INNER JOIN
25+
diadesatlas.basin_outlet bo
26+
USING (basin_id);" ) %>%
2627
tibble()
27-
28-
29-
# write_rds(data_catchment, './data_input/data_catchment.rds')
30-
} else {
31-
data_catchment <- read_rds('./data_input/data_catchment.rds')
32-
}
28+
3329
# ---------------------------------------------------------------------- #
3430
## Distances between catchment ----
35-
if ( connection_sql) {
36-
outlet_distance = dbGetQuery(conn_eurodiad,"SELECT
31+
outlet_distance = dbGetQuery(conn_eurodiad,
32+
"SELECT
3733
b.basin_name AS departure,
3834
od.departure AS departure_id,
3935
b2.basin_name AS arrival,
@@ -48,37 +44,54 @@ INNER JOIN diadesatlas.basin b2 ON
4844
ORDER BY departure, distance ;") %>%
4945
tibble()
5046

51-
# write_rds(outlet_distance, "./data_input/outletDistance.rds")
52-
} else {
53-
outlet_distance <- read_rds( "./data_input/outletDistance.rds")
54-
}
5547

5648
# ---------------------------------------------------------------------- #
5749
# HyDiaD parameters ----
58-
if (connection_sql) {
59-
hydiad_parameter <- dbGetQuery(conn_eurodiad, "
60-
SELECT s.latin_name, s.local_name AS \"Lname\", h.* FROM diadesatlas.hydiadparameter h
61-
INNER JOIN diadesatlas.species s USING (species_id);") %>%
50+
hydiad_parameter <-
51+
dbGetQuery(conn_eurodiad,
52+
"SELECT
53+
s.latin_name,
54+
s.local_name AS \"Lname\",
55+
h.*
56+
FROM
57+
diadesatlas.hydiadparameter h
58+
INNER JOIN
59+
diadesatlas.species s
60+
USING (species_id);") %>%
6261
tibble()
63-
64-
# hydiad_parameter %>% write_rds("./data_input/HyDiaDParameter.rds")
6562

66-
} else {
67-
hydiad_parameter <- read_rds("./data_input/HyDiaDParameter.rds")
68-
}
6963

7064

7165
# ---------------------------------------------------------------------- #
7266
## HSI abd Nmax ----
73-
if (connection_sql) {
74-
# a query to load HSI for only 8.5 scenario (which do not change between simulations)
75-
query = "SELECT s.latin_name, basin_id, basin_name, country, surface_area_drainage_basin as surface_area, year, climatic_scenario, climatic_model_code, hsi FROM diadesatlas.hybrid_model_result hmr
76-
INNER JOIN diadesatlas.species s USING (species_id)
77-
INNER JOIN diadesatlas.basin b USING (basin_id)
78-
INNER JOIN diadesatlas.climatic_model cm USING (climatic_model_id)
79-
WHERE year > 0 AND climatic_scenario = 'rcp85'"
80-
81-
data_hsi_nmax <- dbGetQuery(conn_eurodiad, query) %>%
67+
# a query to load HSI for only 8.5 scenario (which do not change between simulations)
68+
query =
69+
"SELECT
70+
s.latin_name,
71+
basin_id,
72+
basin_name,
73+
country,
74+
surface_area_drainage_basin as surface_area,
75+
year,
76+
climatic_scenario,
77+
climatic_model_code,
78+
hsi
79+
FROM
80+
diadesatlas.hybrid_model_result hmr
81+
INNER JOIN
82+
diadesatlas.species s
83+
USING (species_id)
84+
INNER JOIN
85+
diadesatlas.basin b
86+
USING (basin_id)
87+
INNER JOIN
88+
diadesatlas.climatic_model cm
89+
USING (climatic_model_id)
90+
WHERE
91+
year > 0
92+
AND climatic_scenario = 'rcp85'"
93+
94+
data_hsi_nmax <- dbGetQuery(conn_eurodiad, query) %>%
8295
tibble() %>%
8396
# compute the maximum abundance (#) according to hsi,
8497
# maximal density (Dmax) , catchment area (ccm_area)
@@ -88,53 +101,75 @@ WHERE year > 0 AND climatic_scenario = 'rcp85'"
88101
mutate(Nmax = hsi * Dmax * surface_area) %>%
89102
select(-c(surface_area, Dmax))
90103

91-
# write_rds(data_hsi_nmax, './data_input/data_hsi_Nmax.rds')
92-
93-
rm(query)
94-
} else {
95-
data_hsi_nmax <- read_rds('./data_input/data_hsi_Nmax.rds')
96-
}
104+
rm(query)
97105

98106

99107
# No ccm_area for Bou_Regreg, Loukkos, Oum_er_Rbia, Sebou. use surface_area_drainage_basin
100108

101109
# reference results
102-
if (connection_sql) {
103-
reference_results <- dbGetQuery(conn_eurodiad,
104-
"SELECT s.latin_name, basin_id, basin_name, year, climatic_scenario, climatic_model_code, nit FROM diadesatlas.hybrid_model_result hmr
105-
INNER JOIN diadesatlas.species s USING (species_id)
106-
INNER JOIN diadesatlas.basin b USING (basin_id)
107-
INNER JOIN diadesatlas.climatic_model cm USING (climatic_model_id)
108-
WHERE year > 0 AND climatic_scenario = 'rcp85'
109-
ORDER BY latin_name, basin_id, climatic_model_code") %>%
110+
reference_results <- dbGetQuery(conn_eurodiad,
111+
"SELECT
112+
s.latin_name,
113+
basin_id,
114+
basin_name,
115+
year,
116+
climatic_scenario,
117+
climatic_model_code,
118+
nit
119+
FROM
120+
diadesatlas.hybrid_model_result hmr
121+
INNER JOIN
122+
diadesatlas.species s
123+
USING (species_id)
124+
INNER JOIN
125+
diadesatlas.basin b
126+
USING (basin_id)
127+
INNER JOIN
128+
diadesatlas.climatic_model cm
129+
USING (climatic_model_id)
130+
WHERE
131+
year > 0 AND
132+
climatic_scenario = 'rcp85'
133+
ORDER BY
134+
latin_name,
135+
basin_id,
136+
climatic_model_code") %>%
110137
tibble()
111138

112-
# write_rds(reference_results, './data_input/referenceResults.rds')
113-
} else {
114-
reference_results <- read_rds('./data_input/referenceResults.rds')
115-
}
116-
117-
118139
## initial abundance in catchments ----
119-
if (connection_sql) {
120-
data_ni0 <- dbGetQuery(conn_eurodiad, "SELECT s.latin_name, basin_id, basin_name, surface_area_drainage_basin as surface_area, year, climatic_scenario, climatic_model_code, nit, hsi FROM diadesatlas.hybrid_model_result hmr
121-
INNER JOIN diadesatlas.species s USING (species_id)
122-
INNER JOIN diadesatlas.basin b USING (basin_id)
123-
INNER JOIN diadesatlas.climatic_model cm USING (climatic_model_id)
124-
WHERE climatic_scenario = 'rcp85'
125-
AND year = 0
126-
ORDER BY latin_name, basin_id, climatic_model_code") %>%
140+
data_ni0 <-
141+
dbGetQuery(conn_eurodiad,
142+
"SELECT
143+
s.latin_name,
144+
basin_id,
145+
basin_name,
146+
surface_area_drainage_basin as surface_area,
147+
year,
148+
climatic_scenario,
149+
climatic_model_code,
150+
nit,
151+
hsi
152+
FROM
153+
diadesatlas.hybrid_model_result hmr
154+
INNER JOIN
155+
diadesatlas.species s
156+
USING (species_id)
157+
INNER JOIN
158+
diadesatlas.basin b
159+
USING (basin_id)
160+
INNER JOIN
161+
diadesatlas.climatic_model cm
162+
USING (climatic_model_id)
163+
WHERE
164+
climatic_scenario = 'rcp85'
165+
AND year = 0
166+
ORDER BY
167+
latin_name,
168+
basin_id,
169+
climatic_model_code") %>%
127170
tibble() %>%
128171
inner_join(hydiad_parameter %>%
129172
select(latin_name, Dmax),
130173
by = 'latin_name') %>%
131174
mutate(Nmax = hsi * Dmax * surface_area) %>%
132175
select(-c(surface_area, Dmax))
133-
134-
# write_rds(data_ni0, './data_input/data_ni0.rds')
135-
} else {
136-
data_ni0 <- read_rds('./data_input/data_ni0.rds')
137-
}
138-
139-
#
140-

0 commit comments

Comments
 (0)