library(flexdashboard) library(shiny) library(dplyr) library(XML) library(DBI) library(RPostgres) library(RPostgreSQL) library(dplyr) library(leaflet) library(leaflet.extras) library(leafpm) library(mapedit) library(sf) library(sp) library(plotly) library(ggplot2) library(plyr)
####################### ##Database Identifiers: DRV=RPostgres::Postgres() source(file = "~/Desktop/CODES/IRDTunaAtlas/credentials.R") #################################################################################################################################################################################################################################### source("https://raw.githubusercontent.com/juldebar/IRDTunaAtlas/master/R/TunaAtlas_i6_SpeciesMap.R") source("https://raw.githubusercontent.com/juldebar/IRDTunaAtlas/master/R/TunaAtlas_i7_SpeciesMapRelativeCatches.R") source("https://raw.githubusercontent.com/juldebar/IRDTunaAtlas/master/R/TunaAtlas_i8_SpeciesMapRelativeCatchesOtherSpecies.R") ## server side code function(input, output, session) { output$table <- DT::renderDataTable({ # query <- sqlInterpolate(ANSI(),"SELECT ogc_fid, geom_id, geom, year, species, country, value, count FROM i6i7i8 WHERE species IN ('?species') AND year IN ('?year');", # species =paste0(input$year_choice_i6i7,collapse="','"), year = paste0(input$year_choice_i6i7,collapse="','")) df_i6i7 = st_read(con, query = paste0("SELECT ogc_fid, geom_id, geom, year, species, country, value, count FROM public.i6i7i8 WHERE ST_Within(geom,ST_GeomFromText('",wkt(),"',4326)) AND species IN ('",paste0(input$species_i6i7i8,collapse="','"),"') AND year IN ('",paste0(input$year_choice_i6i7,collapse="','"),"');") ) outp <- dbGetQuery(pool, query) ret <- DT::datatable(outp) return(ret) }) }
# Check http://jeffgoldsmith.com/p8105_f2017/shiny.html new_wkt <- 'POLYGON((-180 -90, 180 -90, 180 90, -180 90, -180 -90))' wkt <- reactiveVal(new_wkt) target_species_i8<- c("YFT","BET","SKJ") target_species_i6i7<- c("YFT") # target_year <- c(seq(1:10)+1994) target_year<- c(2000) target_year_i8 <- c(1999,2000) query_i6i7i8 <- paste0("SELECT ogc_fid, geom_id, geom, year, species, country, value, count FROM i6i7i8 WHERE species IN ('",paste0(target_species_i8,collapse="','"),"') AND year IN ('",paste0(target_year,collapse="','"),"');") df_i6i7i8 = st_read(con, query = query_i6i7i8) year_i6i7i8 = unique(df_i6i7i8$year) species_i6i7i8 = unique(df_i6i7i8$species) # selectInput widget selectInput("species_i6i7i8", label = h3("Select species for i8 (at least two species):"), choices = species_i6i7i8, selected = target_species_i8, multiple = TRUE) selectInput("year_choice_i8", label = h3("Select year for i8 (if multiple years, generates one map per year):"), choices = year_i6i7i8, selected = target_year_i8,multiple = TRUE) selectInput("species_i6i7i8", label = h3("Select species for i6, i7"), choices = species_i6i7i8, selected = target_species_i6i7, multiple = FALSE) selectInput("year_choice_i6i7", label = h3("Select year for i6, i7"), choices = year_i6i7i8, selected = target_year, multiple = FALSE) # textOutput(wkt()) renderPrint({ paste0("The area you have selected is (WKT format): ", wkt()) query_i6i7i8 <- paste0("SELECT ogc_fid, geom_id, geom, year, species, country, value, count FROM public.i6i7i8 WHERE ST_Within(geom,ST_GeomFromText('",wkt(),"',4326)) AND species IN ('",paste0(input$species_i6i7i8,collapse="','"),"') AND year IN ('",paste0(input$year_choice_i8,collapse="','"),"');") }) # renderDataTable( # # )
To be done : add all WMS layers of input datasets
output$mymap <- renderLeaflet( leaflet() %>% addTiles() %>% # addPmToolbar( # toolbarOptions = pmToolbarOptions(drawMarker = FALSE, position = "topright"), # drawOptions = pmDrawOptions(snappable = FALSE, allowSelfIntersection = FALSE), # editOptions = pmEditOptions(preventMarkerRemoval = TRUE, draggable = FALSE), # cutOptions = pmCutOptions(snappable = FALSE, allowSelfIntersection = FALSE) # ) %>% addDrawToolbar( targetGroup = "draw", editOptions = editToolbarOptions( selectedPathOptions = selectedPathOptions() ) ) %>% setView(lng =48, lat =-8, zoom = 5 ) %>% addWMSTiles( # "http://thredds.oreme.org:8080/thredds/wms/MARBEC/Seychelles/global-reanalysis-phy-001-030-monthly_SEY_t55_199301-201906.nc", "https://geoserver-sdi-lab.d4science.org/geoserver/sdilab_geoflow/ows?service=WMS", layers = "global_catch_5deg_1m_ird_level0", # layers = "thetao", options = WMSTileOptions(format = "image/png", transparent = TRUE), group ="Seatizen", attribution = "Seatizen WMS" ) %>% addLayersControl( overlayGroups = c("draw"), options = layersControlOptions(collapsed = FALSE) ) ) leafletOutput('mymap') observe({ #use the draw_stop event to detect when users finished drawing feature <- input$mymap_draw_new_feature req(input$mymap_draw_stop) print(feature) polygon_coordinates <- input$mymap_draw_new_feature$geometry$coordinates[[1]] # see https://rstudio.github.io/leaflet/shiny.html bb <- input$mymap_bounds geom_polygon <- input$mymap_draw_new_feature$geometry # drawn_polygon <- Polygon(do.call(rbind,lapply(polygon_coordinates,function(x){c(x[[1]][1],x[[2]][1])}))) geoJson <- geojsonio::as.json(feature) # spdf <- geojsonio::geojson_sp(feature) geom <- st_read(geoJson) wkt(st_as_text(st_geometry(geom[1,]))) coord <- st_as_text(st_geometry(geom[1,])) north <- polygon_coordinates[[1]][[1]] south <- polygon_coordinates[[2]][[1]] east <- polygon_coordinates[[1]][[2]] west <- polygon_coordinates[[2]][[2]] if(is.null(polygon_coordinates)) return() text<-paste("North ", north, "South ", east) mymap_proxy = leafletProxy("mymap") %>% clearPopups() %>% addPopups(south,west,coord) textOutput("wkt") if(wkt()!=new_wkt){ df_i6i7i8 = st_read(con, query = paste0("SELECT ogc_fid, geom_id, geom, year, species, country, value, count FROM public.i6i7i8 WHERE ST_Within(geom,ST_GeomFromText('",wkt(),"',4326)) AND species IN ('",paste0(input$species_i6i7i8,collapse="','"),"') AND year IN ('",paste0(input$year_choice_i6i7,collapse="','"),"');") ) } })
# https://plotly.com/r/maps-sf/ # https://ggplot2.tidyverse.org/reference/ggsf.html output$plot8 <- renderPlotly({ if(wkt()!=new_wkt){ df_i8 = st_read(con, query = paste0("SELECT ogc_fid, geom_id, geom, year, species, country, value, count FROM public.i6i7i8 WHERE ST_Within(geom,ST_GeomFromText('",wkt(),"',4326)) AND species IN ('",paste0(input$species_i6i7i8,collapse="','"),"') AND year IN ('",paste0(input$year_choice_i8,collapse="','"),"');") ) }else{ df_i8=df_i6i7i8 } # df_i6i7i8_filtered <- df_i6i7i8 %>% filter(year %in% target_year, species %in% target_species) # df_i6i7i8_filtered <- df_i6i7i8 %>% filter(year %in% input$year_choice_i8, species %in% input$species_i6i7i8) df_i8_filtered <- as(df_i8, "Spatial") i8 <- Atlas_i8_SpeciesMapRelativeCatchesOtherSpecies(df=df_i8_filtered, targetedSpecies=input$species_i6i7i8[1], geomIdAttributeName="geom_id", yearAttributeName="year", speciesAttributeName="species", valueAttributeName="value", withSparql=FALSE ) # i8 <- plot_geo(df_i8_filtered) }) plotlyOutput("plot8")
output$plot7 <- renderPlotly({ if(wkt()!=new_wkt){ df_i6i7 = st_read(con, query = paste0("SELECT ogc_fid, geom_id, geom, year, species, country, value, count FROM public.i6i7i8 WHERE ST_Within(geom,ST_GeomFromText('",wkt(),"',4326)) AND species IN ('",paste0(input$species_i6i7i8,collapse="','"),"') AND year IN ('",paste0(input$year_choice_i6i7,collapse="','"),"');") ) }else{ df_i6i7=df_i6i7i8 } # df_i6i7i8_filtered <- df_i6i7i8 %>% filter(year %in% input$year_choice_i8, species %in% input$species_i6i7i8) #Filtering input dataset with selected parameters # df_i6i7i8_filtered <- df_i6i7i8 %>% filter(year %in% input$year_choice_i6i7, species %in% input$species_i6i7i8) df_i7_filtered <- as(df_i6i7, "Spatial") i7 <- Atlas_i7_SpeciesMapRelativeCatches(df=df_i7_filtered, geomIdAttributeName="geom_id", yearAttributeName="year", speciesAttributeName="species", valueAttributeName="value", withSparql=FALSE ) # i7 }) plotlyOutput("plot7")
output$plot6 <- renderPlotly({ if(wkt()!=new_wkt){ df_i6i7 = st_read(con, query = paste0("SELECT ogc_fid, geom_id, geom, year, species, country, value, count FROM public.i6i7i8 WHERE ST_Within(geom,ST_GeomFromText('",wkt(),"',4326)) AND species IN ('",paste0(input$species_i6i7i8,collapse="','"),"') AND year IN ('",paste0(input$year_choice_i6i7,collapse="','"),"');") ) }else{ df_i6i7=df_i6i7i8 } # if(wkt()!=new_wkt){ # df_i6i7i8 = st_read(con, # query = paste0("SELECT ogc_fid, geom_id, geom, year, species, country, value, count FROM public.i6i7i8 WHERE ST_Within(geom,ST_GeomFromText('",wkt(),"',4326)) AND species IN ('",paste0(input$species_i6i7i8,collapse="','"),"') AND year IN ('",paste0(input$year_choice_i6i7,collapse="','"),"');") # ) # }else{ # df_i6i7 <- df_i6i7i8 # } # df_i6i7i8_filtered <- df_i6i7i8 %>% filter(year %in% input$year_choice_i6i7, species %in% input$species_i6i7i8) df_i6_filtered <- as(df_i6i7, "Spatial") i6 <- Atlas_i6_SpeciesMap(df=df_i6_filtered, geomIdAttributeName="geom_id", yearAttributeName="year", speciesAttributeName="species", valueAttributeName="value", withSparql=FALSE ) }) plotlyOutput("plot6")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.