<!DOCTYPE html>

bd-page3-climate-change

bd-page3-climate-change

library(diades.atlas)
# renv::install("dm")
# library(dm)
library(dplyr)
library(leaflet)
library(ggplot2)

Connect to database

Do not forget to set environment variables in .Renviron

pkgload::load_all(attach_testthat = FALSE)
#> ℹ Loading diades.atlas
session <- new.env()
connect(session)
library(leaflet)
library(shiny)
library(tmap)
library(dbplyr)
library(dplyr)
input <- new.env()
c(
  dataCatchment,
  catchment_geom,
  dataALL,
  ices_geom,
  species_list
) %<-% generate_datasets(
  get_con(
    session
  )
)
#> ── generate_datasets ───────────────────────────────────

species_id <- species_list[species_list$latin_name == "alosa-fallax", "species_id"]

scenario <- "rcp85"
date <- c(1970, 2010)

hybrid_model_result <- tbl(get_con(session), "hybrid_model_result")

# get_hybrid_model() in R/mod_c_second_fct_query_and_plot.R
model_res <- get_hybrid_model(
  species_id = 6,
  scenario = "rcp85",
  # date = c(1970, 2010),
  session = session,
  lg = "fr"
)

tbl(
  get_con(session),
  "hybrid_model_result"
) %>% distinct(species_id)
#> # Source:   lazy query [?? x 1]
#> # Database: postgres
#> #   [diadesatlas_owner@localhost:5432/diades]
#>    species_id
#>         <int>
#>  1         22
#>  2         20
#>  3          8
#>  4         19
#>  5         13
#>  6         25
#>  7         18
#>  8          4
#>  9         21
#> 10          6
#> # … with more rows

# Available_bv
bv_ids <- sort(
  unique(model_res$basin_id)
)

get_bv_geoms <- function(bv_ids,
                         lg,
                         session = shiny::getDefaultReactiveDomain()) {
  res <- sf::st_read(
    get_con(session),
    query = sprintf(
      "select basin_id, verysimplified_geom, ccm_name from basin_outlet where basin_id IN %s",
      dbplyr::translate_sql(!!bv_ids)
    )
  )
  basin <- tbl(get_con(session), "basin") %>%
    filter(basin_id %in% !!res$basin_id) %>%
    mutate(basin_name = diadesatlas.translate(basin_name, !!lg)) %>%
    select(basin_id, basin_name) %>%
    collect()
  
  res %>%
  left_join(basin, by = "basin_id")
}

bv_df <- get_bv_geoms(
  bv_ids,
  'fr',
  session
)
year <- 1951


# plot hsi nit ----

selected_bv <- 385
selected_year <- 2002


# Plot leaflet
bv_df <- get_bv_geoms(
  bv_ids,
  'fr',
  session
)
draw_bv_leaflet(bv_df, model_res, 1976)

Test separate shiny module

ui <- function(request) {
  tagList(
    leaflet::leafletOutput("plot")
  )
}

server <- function(input,
                   output,
                   session) {
  output$plot <- leaflet::renderLeaflet({
    draw_bv_leaflet(bv_df, model_res, 1976)
  })

  observeEvent(input$plot_shape_click, { # update the location selectInput on map clicks
    print(input$plot_shape_click)
  })
}

shinyApp(ui, server)

Stop connection

# DBI::dbDisconnect(con)
DBI::dbDisconnect(session$userData$con)